@mux/mux-elements-codemod
Advanced tools
Comparing version 1.0.1 to 1.0.2-canary.0-8eff156
{ | ||
"name": "@mux/mux-elements-codemod", | ||
"version": "1.0.1", | ||
"version": "1.0.2-canary.0-8eff156", | ||
"description": "A codemod to transform @mux-elments scope imports into @mux scope imports", | ||
@@ -75,4 +75,3 @@ "bin": "./dist/index.mjs", | ||
} | ||
}, | ||
"gitHead": "712cdfba61ad6ae3f58e91881e7d3dc605ebbb4c" | ||
} | ||
} |
@@ -81,1 +81,66 @@ <p align="center"> | ||
``` | ||
## Package update | ||
This command will allow you to more easily remove the old scoped packages (`@mux-elements/`) and re-add them via the new scope (`@mux`). | ||
If the current working directory has a `package.json`, it will assume it's a module and will update the dependencies. | ||
It will assume that `yarn` is being used when if a `yarn.lock` is present, otherwise, can be told to use a specific client via `--npm client yarn`. | ||
```sh | ||
$ mux-elements-codemod --package | ||
The following dependencies will be removed and re-added with the updated @mux/ scope: | ||
@mux-elements/mux-audio | ||
@mux-elements/mux-audio-react | ||
@mux-elements/mux-player | ||
@mux-elements/mux-player-react | ||
@mux-elements/mux-video | ||
@mux-elements/mux-video-react | ||
``` | ||
And then you can run it with `--force` to apply the changes | ||
```sh | ||
mux-elements-codemod -p --force | ||
Running yarn remove on @mux-elements/mux-audio @mux-elements/mux-audio-react @mux-elements/mux-player @mux-elements/mux-player-react @mux-elements/mux-video @mux-elements/mux-video-react | ||
yarn remove v1.22.17 | ||
[1/7] Removing module @mux-elements/mux-audio... | ||
[2/7] Removing module @mux-elements/mux-audio-react... | ||
[3/7] Removing module @mux-elements/mux-player... | ||
[4/7] Removing module @mux-elements/mux-player-react... | ||
[5/7] Removing module @mux-elements/mux-video... | ||
[6/7] Removing module @mux-elements/mux-video-react... | ||
[7/7] Regenerating lockfile and installing missing dependencies... | ||
success Uninstalled packages. | ||
Done in 0.39s. | ||
Running yarn add on @mux-elements/mux-audio @mux-elements/mux-audio-react @mux-elements/mux-player @mux-elements/mux-player-react @mux-elements/mux-video @mux-elements/mux-video-react | ||
yarn add v1.22.17 | ||
[1/4] Resolving packages... | ||
[2/4] Fetching packages... | ||
[3/4] Linking dependencies... | ||
[4/4] Building fresh packages... | ||
success Saved lockfile. | ||
success Saved 11 new dependencies. | ||
info Direct dependencies | ||
├─ @mux/mux-audio-react@0.4.0 | ||
├─ @mux/mux-audio@0.6.0 | ||
├─ @mux/mux-player-react@0.1.0-beta.22 | ||
├─ @mux/mux-player@0.1.0-beta.22 | ||
├─ @mux/mux-video-react@0.5.0 | ||
└─ @mux/mux-video@0.8.1 | ||
info All dependencies | ||
├─ @github/template-parts@0.5.3 | ||
├─ @mux/mux-audio-react@0.4.0 | ||
├─ @mux/mux-audio@0.6.0 | ||
├─ @mux/mux-player-react@0.1.0-beta.22 | ||
├─ @mux/mux-player@0.1.0-beta.22 | ||
├─ @mux/mux-video-react@0.5.0 | ||
├─ @mux/mux-video@0.8.1 | ||
├─ hls.js@1.1.5 | ||
├─ media-chrome@0.6.9 | ||
├─ mux-embed@4.9.4 | ||
└─ react-is@16.13.1 | ||
Done in 7.31s. | ||
Replacing @mux-elements scope to @mux in package succeeded successfully! 🎉 | ||
``` |
122
src/index.ts
@@ -12,3 +12,3 @@ #!/usr/bin/env node | ||
const args = minimist(argv.slice(2), { | ||
string: ['extensions'], | ||
string: ['extensions', 'npm-client'], | ||
boolean: ['imports'], | ||
@@ -19,2 +19,3 @@ default: { | ||
force: false, | ||
package: false, | ||
}, | ||
@@ -25,2 +26,4 @@ alias: { | ||
f: 'force', | ||
n: 'npm-client', | ||
p: 'package', | ||
}, | ||
@@ -33,3 +36,9 @@ }); | ||
const force = args.force; | ||
let npmClient: 'npm' | 'yarn' = args['npm-client'] ?? 'npm'; | ||
// default npm client to yarn if yarn.lock is present | ||
if (!args['npm-client'] && sh.test('-f', './yarn.lock')) { | ||
npmClient = 'yarn'; | ||
} | ||
if (paths.length === 0) { | ||
@@ -59,4 +68,8 @@ paths.push('./'); | ||
default is "js ts jsx tsx html mjs cjs" | ||
-f --force by default, this does a dry run, run with --force to replace the text inline | ||
-f --force by default, this does a dry run, run with --force to apply changes | ||
-h --help show this help | ||
-p --package Update the package.json in the current working directory to: | ||
- remove existing dependencies and dev dependencies of @mux-elements/ scope | ||
- add the packages with the new @mux/ scope | ||
-n --npm-client set an npm client. By default it is npm unless a yarn.lock file is present, in which case it is yarn. | ||
` | ||
@@ -149,2 +162,105 @@ ); | ||
const updatePackage = () => { | ||
if (!sh.test('-f', './package.json')) { | ||
sh.echo(chalk.red("This folder isn't a module an doesn't include a package.json file.")); | ||
sh.exit(1); | ||
return; | ||
} | ||
const pkg = JSON.parse(sh.cat('./package.json').toString()); | ||
const muxElDeps = Object.keys(pkg.dependencies).filter((dep) => dep.startsWith('@mux-elements/')) as string[]; | ||
const muxElDevDeps = Object.keys(pkg.devDependencies).filter((dep) => dep.startsWith('@mux-elements/')) as string[]; | ||
type ExecType = 'dep' | 'dev'; | ||
type Command = 'add' | 'remove'; | ||
const getExec = (type: ExecType, command: Command, deps: string[]) => { | ||
const execOptions = [npmClient, '--color=always', command]; | ||
if (command === 'add') { | ||
if (type === 'dev') { | ||
if (npmClient === 'yarn') { | ||
execOptions.push('--dev'); | ||
} else { | ||
execOptions.push('--save-dev'); | ||
} | ||
} else if (type === 'dep' && npmClient === 'npm') { | ||
execOptions.push('--save'); | ||
} | ||
} | ||
return execOptions.concat(deps); | ||
}; | ||
const errors = []; | ||
let code, stdout, stderr; | ||
if (muxElDeps.length) { | ||
if (force) { | ||
sh.echo(`Running ${npmClient} remove on ${muxElDeps.join(' ')}`); | ||
({ code, stdout, stderr } = sh.exec(getExec('dep', 'remove', muxElDeps).join(' '))); | ||
errors.push(['remove dependencies', code, stdout, stderr]); | ||
sh.echo(`Running ${npmClient} add on ${muxElDeps.join(' ')}`); | ||
({ code, stdout, stderr } = sh.exec( | ||
getExec( | ||
'dep', | ||
'add', | ||
muxElDeps.map((dep) => dep.replace('@mux-elements/', '@mux/')) | ||
).join(' ') | ||
)); | ||
errors.push(['add dependencies', code, stdout, stderr]); | ||
} else { | ||
sh.echo('The following dependencies will be removed and re-added with the updated @mux/ scope:'); | ||
muxElDeps.forEach((dep) => { | ||
sh.echo('\t', chalk.yellow(dep)); | ||
}); | ||
} | ||
} | ||
if (muxElDevDeps.length) { | ||
if (force) { | ||
sh.echo(`Running ${npmClient} remove on ${muxElDevDeps.join(' ')}`); | ||
({ code, stdout, stderr } = sh.exec(getExec('dev', 'remove', muxElDevDeps).join(' '))); | ||
errors.push(['remove dev dependencies', code, stdout, stderr]); | ||
sh.echo(`Running ${npmClient} add on ${muxElDevDeps.join(' ')}`); | ||
({ code, stdout, stderr } = sh.exec( | ||
getExec( | ||
'dev', | ||
'add', | ||
muxElDevDeps.map((dep) => dep.replace('@mux-elements/', '@mux/')) | ||
).join(' ') | ||
)); | ||
errors.push(['add dev dependencies', code, stdout, stderr]); | ||
} else { | ||
sh.echo('The following dev dependencies will be removed and re-added with the updated @mux/ scope:'); | ||
muxElDevDeps.forEach((dep) => { | ||
sh.echo('\t', chalk.yellow(dep)); | ||
}); | ||
} | ||
} | ||
sh.echo(); | ||
let err = false; | ||
errors.forEach(([scenario, _code, _stdout, _stderr]) => { | ||
if (_code !== 0) { | ||
sh.echo(chalk.red(`scenario ${chalk.bold(scenario)} exited with code`), String(_code)); | ||
// if (verbose === '--verbose' || verbose === '-v') { | ||
sh.echo(chalk.dim(chalk.red(_stdout))); | ||
sh.echo(chalk.dim(chalk.red(_stderr))); | ||
// } | ||
err = true; | ||
} | ||
}); | ||
if (err) { | ||
sh.exit(1); | ||
} else { | ||
if (force) { | ||
sh.echo(chalk.green('Replacing @mux-elements scope to @mux in package succeeded successfully! 🎉')); | ||
} | ||
} | ||
}; | ||
if (args.help) { | ||
@@ -154,4 +270,6 @@ printHelp(); | ||
imports(); | ||
} else if (args.package) { | ||
updatePackage(); | ||
} else { | ||
printHelp(); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
41828
458
146
8
2