Bnaya / typescript-monorepo-toolkit

CLI tool to automate typescript mono-repo management
38 stars 3 forks source link

Unexpected token y in JSON at position 0 #1

Open thekevinbrown opened 4 years ago

thekevinbrown commented 4 years ago

This is what I'm getting when I run npx typescript-monorepo-toolkit inject-refs .:

❯ DEBUG=typescript-monorepo-toolkit npx typescript-monorepo-toolkit inject-refs .
npx: installed 15 in 1.755s
  typescript-monorepo-toolkit Running command inject-refs with { yarnWorkspaceRoot: '.',
  typescript-monorepo-toolkit   cmdObj:
  typescript-monorepo-toolkit    Command {
  typescript-monorepo-toolkit      commands: [],
  typescript-monorepo-toolkit      options: [ [Option] ],
  typescript-monorepo-toolkit      _execs: Set {},
  typescript-monorepo-toolkit      _allowUnknownOption: false,
  typescript-monorepo-toolkit      _args: [ [Object] ],
  typescript-monorepo-toolkit      _name: 'inject-refs',
  typescript-monorepo-toolkit      _optionValues: {},
  typescript-monorepo-toolkit      _storeOptionsAsProperties: true,
  typescript-monorepo-toolkit      _passCommandToAction: true,
  typescript-monorepo-toolkit      _actionResults: [],
  typescript-monorepo-toolkit      _helpFlags: '-h, --help',
  typescript-monorepo-toolkit      _helpDescription: 'output usage information',
  typescript-monorepo-toolkit      _helpShortFlag: '-h',
  typescript-monorepo-toolkit      _helpLongFlag: '--help',
  typescript-monorepo-toolkit      _noHelp: false,
  typescript-monorepo-toolkit      _exitCallback: undefined,
  typescript-monorepo-toolkit      _executableFile: undefined,
  typescript-monorepo-toolkit      parent:
  typescript-monorepo-toolkit       Command {
  typescript-monorepo-toolkit         commands: [Array],
  typescript-monorepo-toolkit         options: [Array],
  typescript-monorepo-toolkit         _execs: Set {},
  typescript-monorepo-toolkit         _allowUnknownOption: false,
  typescript-monorepo-toolkit         _args: [],
  typescript-monorepo-toolkit         _name: 'typescript-monorepo-toolkit',
  typescript-monorepo-toolkit         _optionValues: {},
  typescript-monorepo-toolkit         _storeOptionsAsProperties: true,
  typescript-monorepo-toolkit         _passCommandToAction: true,
  typescript-monorepo-toolkit         _actionResults: [],
  typescript-monorepo-toolkit         _helpFlags: '-h, --help',
  typescript-monorepo-toolkit         _helpDescription: 'output usage information',
  typescript-monorepo-toolkit         _helpShortFlag: '-h',
  typescript-monorepo-toolkit         _helpLongFlag: '--help',
  typescript-monorepo-toolkit         _version: '0.0.6',
  typescript-monorepo-toolkit         _versionOptionName: 'version',
  typescript-monorepo-toolkit         _events: [Object],
  typescript-monorepo-toolkit         _eventsCount: 5,
  typescript-monorepo-toolkit         tsconfigPath: 'tsconfig.json',
  typescript-monorepo-toolkit         rawArgs: [Array],
  typescript-monorepo-toolkit         args: [Array] },
  typescript-monorepo-toolkit      _description:
  typescript-monorepo-toolkit       'Inject the appropriate tsconfig references based on yarn workspaces dependency graph',
  typescript-monorepo-toolkit      _argsDescription: undefined,
  typescript-monorepo-toolkit      generateBuildAll: false,
  typescript-monorepo-toolkit      _events:
  typescript-monorepo-toolkit       [Object: null prototype] { 'option:generate-build-all': [Function] },
  typescript-monorepo-toolkit      _eventsCount: 1 } } +0ms
command exited with an error SyntaxError: Unexpected token y in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.injectRefs (/Users/kevin/.npm/_npx/12085/lib/node_modules/typescript-monorepo-toolkit/dist/commands.js:10:49)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Digging into the code a bit, I don't see any data key in my yarn workspaces info output. It's like this:

{
  "My_Project_Name/consumer-api-docs": {
    "location": "packages/consumer-api-docs",
    "workspaceDependencies": [
      "My_Project_Name/consumer-api"
    ],
    "mismatchedWorkspaceDependencies": []
  },
  "My_Project_Name/consumer-api": {
    "location": "packages/consumer-api",
    "workspaceDependencies": [
      "My_Project_Name/database-entities"
    ],
    "mismatchedWorkspaceDependencies": []
  },
  "My_Project_Name/database-entities": {
    "location": "packages/database-entities",
    "workspaceDependencies": [
      "My_Project_Name/logger"
    ],
    "mismatchedWorkspaceDependencies": []
  },
  "My_Project_Name/infrastructure": {
    "location": "packages/infrastructure",
    "workspaceDependencies": [
      "My_Project_Name/consumer-api",
      "My_Project_Name/database-entities",
      "My_Project_Name/logger",
      "My_Project_Name/migration-runner",
      "My_Project_Name/retailer-api"
    ],
    "mismatchedWorkspaceDependencies": []
  },
  "My_Project_Name/logger": {
    "location": "packages/logger",
    "workspaceDependencies": [],
    "mismatchedWorkspaceDependencies": []
  },
  "My_Project_Name/migration-runner": {
    "location": "packages/migration-runner",
    "workspaceDependencies": [
      "My_Project_Name/database-entities",
      "My_Project_Name/logger"
    ],
    "mismatchedWorkspaceDependencies": []
  },
  "My_Project_Name/retailer-api-docs": {
    "location": "packages/retailer-api-docs",
    "workspaceDependencies": [
      "My_Project_Name/retailer-api"
    ],
    "mismatchedWorkspaceDependencies": []
  },
  "My_Project_Name/retailer-api": {
    "location": "packages/retailer-api",
    "workspaceDependencies": [
      "My_Project_Name/database-entities"
    ],
    "mismatchedWorkspaceDependencies": []
  }
}

Updating the first line from:

const workspaceInfoObject = JSON.parse(JSON.parse(await helpers_1.execFile("yarn", ["workspaces", "info", "--json"], projectRoot)).data);

to

const workspaceInfoObject = JSON.parse(await helpers_1.execFile("yarn", ["workspaces", "info", "--json"], projectRoot));

seems to fix it. Should I PR this back into the project, or did I miss what the .data is for?

This is with yarn version 1.22.1.

enzoferey commented 4 years ago

Facing the same issue.

Bnaya commented 4 years ago

Thanks for the report! A new version would be released soon, let me know if it fixes your issue!

Bnaya commented 4 years ago

@enzoferey @thekevinbrown let me know if it solves your issue

enzoferey commented 4 years ago

Works for me 👍

In the meantime I have moved to a custom script that gives me a bit more flexibility as I have a monorepo mixing apps and packages (and I don't want apps to get composite: true). Still validating to see that the result of running your script and mine is only this small customization I needed 😄

Bnaya commented 4 years ago

Do you think we should set composite: true only for leaf packages? Do you know about any unwanted effect of setting composite: true to none-root packages?

enzoferey commented 4 years ago

I'm referring to https://github.com/facebook/create-react-app/issues/7147 and https://github.com/facebook/create-react-app/issues/8551.

Bnaya commented 4 years ago

Thanks for the feedback, if it's still a problem for you, can you open a separate feature request?