datocms / cli

Official CLI to interact with DatoCMS projects!
MIT License
7 stars 7 forks source link

Running migration SyntaxError: Unexpected token 'export' #12

Closed marcello-palmitessa closed 1 year ago

marcello-palmitessa commented 1 year ago

Hi, I created a migration script using the automation via CLI, but then whern I try to apply it using: datocms migrations:run --destination=main --api-token=xxxxxxI get the error: Running migration "1670316600_version3.ts"... ! SyntaxError: Unexpected token 'export'

The generated migration script is like this:

import { Client, SimpleSchemaTypes } from '@datocms/cli/lib/cma-client-node';

export default async function (client: Client) {
....
}

Running with macOS Ventura, nodejs v18.2.0

any ideas please?

stefanoverna commented 1 year ago

do you have a tsconfig.json? can you share it?

marcello-palmitessa commented 1 year ago

Sure, here it is, and I forgot to mention, the project is using React 18 and Next.js 13 with the /pages folder:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "@/blocks/*": [
        "blocks/*"
      ],
      "@/components/*": [
        "components/*"
      ],
      "@/lib/*": [
        "lib/*"
      ],
      "@/queries/*": [
        "queries/*"
      ],
      "@/styles/*": [
        "styles/*"
      ],
      "@/types/*": [
        "types/*"
      ]
    },
    "incremental": true
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules",
    "test/*",
    "migrations/*"
  ]
}
stefanoverna commented 1 year ago

@marcello-palmitessa can you please try to temporarly replace the content of your tsconfig with the following, and running the migrations again?

{
  "compilerOptions": {
    "declaration": true,
    "importHelpers": true,
    "module": "commonjs",
    "strict": true,
    "target": "es2019"
  }
}
marcello-palmitessa commented 1 year ago

With that tsconfig I'm getting this error now:

Error: POST https://site-api.datocms.com/plugins: 422 Unprocessable Entity

[ { "id": "631444", "type": "api_error", "attributes": { "code": "INVALID_FIELD", "details": { "field": "name", "code": "VALIDATION_UNIQUENESS", "record_type": "Plugin" } } } ] at /Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@datocms+rest-client-utils@1.1.12/node_modules/@datocms/rest-client-utils/src/request.ts:294:19 at step (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@datocms+rest-client-utils@1.1.12/node_modules/@datocms/rest-client-utils/dist/cjs/request.js:44:23) at Object.next (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@datocms+rest-client-utils@1.1.12/node_modules/@datocms/rest-client-utils/dist/cjs/request.js:25:53) at fulfilled (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@datocms+rest-client-utils@1.1.12/node_modules/@datocms/rest-client-utils/dist/cjs/request.js:16:58) at processTicksAndRejections (node:internal/process/task_queues:95:5) Caused By: Error at Client.request (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@datocms+cma-client@1.1.12/node_modules/@datocms/cma-client/src/generated/Client.ts:138:21) at Plugin.rawCreate (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@datocms+cma-client@1.1.12/node_modules/@datocms/cma-client/src/generated/resources/Plugin.ts:52:24) at Plugin.create (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@datocms+cma-client@1.1.12/node_modules/@datocms/cma-client/src/generated/resources/Plugin.ts:18:17) at default_1 (/Users/EPALMIMGB/projects/tangity-website/migrations/1670344958_updateToVersion3.ts:13:46) at Command.runMigrationScript (/Users/EPALMIMGB/projects/tangity-website/nodemodules/.pnpm/@datocms+cli@1.0.20@types+node@18.11.11/node_modules/@datocms/cli/lib/commands/migrations/run.js:117:23) at async Command.run (/Users/EPALMIMGB/projects/tangity-website/nodemodules/.pnpm/@datocms+cli@1.0.20@types+node@18.11.11/node_modules/@datocms/cli/lib/commands/migrations/run.js:84:13) at async Command._run (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@oclif+core@1.20.4/node_modules/@oclif/core/lib/command.js:80:22) at async Config.runCommand (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@oclif+core@1.20.4/node_modules/@oclif/core/lib/config/config.js:272:25) at async Object.run (/Users/EPALMIMGB/projects/tangity-website/node_modules/.pnpm/@oclif+core@1.20.4/node_modules/@oclif/core/lib/main.js:74:5)

stefanoverna commented 1 year ago

ok, so this is a different type of issue. when the CLI is trying to run the migration, a specific step in the migration file is failing (in particular, a plugin with the same name already exists). it could be either:

  1. a bug in our auto-generation code, or
  2. you're trying to run the migrations in the wrong environment
marcello-palmitessa commented 1 year ago

thanks @stefanoverna I think I found the issue, in the generated script a plugin was first declared and then deleted, bu thte deletion is an error, the plugin is needed:

console.log('Create private plugin "Scribble Editor"'); newPlugins['48468'] = await client.plugins.create({ name: 'Scribble Editor', url: 'https://tangity-scribble-editor.netlify.app/', description: 'Scribble Editor', package_name: null, package_version: null, permissions: [] }); await client.plugins.update(newPlugins['48468'], { parameters: { migratedFromLegacyPlugin: true } });

console.log('Delete plugin "Scribble Editor"'); await client.plugins.destroy('116252');

stefanoverna commented 1 year ago

cool! we should have fixed this bug in the latest CLI version :)