migrate from-package-json
Use the moon migrate from-package-json <project>
sub-command to migrate a project's package.json
to our moon.yml
format. When ran, the following changes are made:
- Converts
package.json
scripts tomoon.yml
tasks. Scripts and tasks are not 1:1, so we'll convert as close as possible while retaining functionality. - Updates
package.json
by removing all converted scripts. If all scripts were converted, the entire block is removed. - Links
package.json
dependencies asmoon.yml
dependencies (dependsOn
). Will map a package's name to their moon project name.
This command is ran per project, and for this to operate correctly, requires all projects to be configured in the workspace. There's also a handful of requirements and caveats to be aware of!
$ moon --log debug migrate from-package-json app
moon does its best to infer the local
option, given the small amount
of information available to use. When this option is incorrectly set, it'll result in CI
environments hanging for tasks that are long-running or never-ending (development servers, etc), or
won't run builds that should be. Be sure to audit each task after migration!
Arguments
<project>
- Name of a project, as defined inprojects
.
Caveats
-
When running a script within another script, the full invocation of
npm run ...
,pnpm run ...
, oryarn run ...
must be used. Shorthand variants are not supported, for example,npm test
oryarn lint
orpnpm format
. We cannot guarantee that moon will parse these correctly otherwise.package.json{
// ...
"scripts": {
"lint": "eslint .",
- "lint:fix": "yarn lint --fix",
+ "lint:fix": "yarn run lint --fix",
}
} -
Scripts that run multiple commands with the AND operator (
&&
) will create an individual transient task for each command, with all tasks linked in-order using taskdeps
. These commands will not run in parallel. For example, given the following script:package.json{
// ...
"scripts": {
// ...
"check": "yarn run lint && yarn run test && yarn run typecheck"
}
}Would create 3 tasks that create the dependency chain:
check-dep1 (lint) -> check-dep2 (test) -> check (typecheck)
, instead of the expected parallel execution oflint | test | typecheck -> check
. If you would prefer these commands to run in parallel, then you'll need to craft your tasks manually. -
Scripts that change directory (
cd ...
), use pipes (|
), redirects (>
), or the OR operator (||
) are not supported and will be skipped. Tasks and scripts are not 1:1 in functionality, as tasks represent that state of a single command execution. However, you can wrap this functionality in a custom script that executes it on the task's behalf. -
Life cycle scripts are not converted to tasks and will remain in
package.json
since they're required by npm (and other package managers). However, their commands will be updated to execute moon commands when applicable.package.json{
// ...
"scripts": {
- "preversion": "yarn run lint && yarn run test",
+ "preversion": "moon run project:lint && moon run project:test",
}
}This does not apply to
run
,start
,stop
, andtest
life cycles. -
"Post" life cycles for user defined scripts do not work, as moon tasks have no concept of "run this after the task completes", so we suggest against using these entirely. However, we still convert the script and include the base script as a task dependency.
For example, a
posttest
script would be converted into aposttest
task, with thetest
task included indeps
. For this to actually run correctly, you'll need to usemoon run <project>:posttest
AND NOTmoon run <project>:test
.