|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +// @ts-expect-error TS can't resolve the type of this import |
| 5 | +import * as jscodeshift from 'jscodeshift/src/Runner'; |
| 6 | +import chalk from 'chalk'; |
| 7 | +import isGitClean from 'is-git-clean'; |
| 8 | +import globby from 'globby'; |
| 9 | +import meow from 'meow'; |
| 10 | + |
| 11 | +const cli = meow({ |
| 12 | + description: 'Code migrations for updating Polaris apps.', |
| 13 | + help: ` |
| 14 | + Usage |
| 15 | + $ npx @shopify/polaris-migrator <migration> <path> <...options> |
| 16 | + migration One of the choices from https://polaris.shopify.com/docs/advanced-features/migrations |
| 17 | + path Files or directory to transform. Can be a glob like src/**.scss |
| 18 | + Options |
| 19 | + --force Bypass Git safety checks and forcibly run migrations |
| 20 | + --dry Dry run (no changes are made to files) |
| 21 | + --print Print transformed files to your terminal |
| 22 | + `, |
| 23 | + flags: { |
| 24 | + force: { |
| 25 | + type: 'boolean', |
| 26 | + }, |
| 27 | + dry: { |
| 28 | + type: 'boolean', |
| 29 | + }, |
| 30 | + print: { |
| 31 | + type: 'boolean', |
| 32 | + }, |
| 33 | + }, |
| 34 | +}); |
| 35 | + |
| 36 | +export function checkGitStatus(force?: boolean) { |
| 37 | + let clean = false; |
| 38 | + let errorMessage = 'Unable to determine if git directory is clean'; |
| 39 | + try { |
| 40 | + clean = isGitClean.sync(process.cwd()); |
| 41 | + errorMessage = 'Git directory is not clean'; |
| 42 | + } catch (err: any) { |
| 43 | + if (err && err.stderr && err.stderr.indexOf('Not a git repository') >= 0) { |
| 44 | + clean = true; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (!clean) { |
| 49 | + /* eslint-disable no-console */ |
| 50 | + if (force) { |
| 51 | + console.log(`WARNING: ${errorMessage}. Forcibly continuing.`); |
| 52 | + } else { |
| 53 | + console.log('Thank you for using @shopify/polaris-migrator!'); |
| 54 | + console.log( |
| 55 | + chalk.yellow( |
| 56 | + '\nBut before we continue, please stash or commit your git changes.', |
| 57 | + ), |
| 58 | + ); |
| 59 | + console.log( |
| 60 | + '\nYou may use the --force flag to override this safety check.', |
| 61 | + ); |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | + /* eslint-enable no-console */ |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +type Args = [ |
| 69 | + migration: string, |
| 70 | + path: string, |
| 71 | + flags?: {force?: boolean; dry?: boolean; print?: boolean}, |
| 72 | +]; |
| 73 | + |
| 74 | +export async function run(...args: Args) { |
| 75 | + const [migration, files] = args.length ? args : cli.input; |
| 76 | + const flags = args && args[2] ? args[2] : cli.flags; |
| 77 | + |
| 78 | + const migrationFile = path.join( |
| 79 | + __dirname, |
| 80 | + `./migrations/${migration}/${migration}.js`, |
| 81 | + ); |
| 82 | + |
| 83 | + try { |
| 84 | + if (!fs.existsSync(migrationFile)) { |
| 85 | + throw new Error(`No migration found for ${migration}`); |
| 86 | + } |
| 87 | + |
| 88 | + if (!files) throw new Error(`No path provided for migration`); |
| 89 | + |
| 90 | + if (!flags.dry) { |
| 91 | + checkGitStatus(cli.flags.force); |
| 92 | + } |
| 93 | + |
| 94 | + const filepaths = globby.sync(files, {cwd: process.cwd()}); |
| 95 | + if (filepaths.length === 0) { |
| 96 | + throw new Error(`No files found for ${files}`); |
| 97 | + } |
| 98 | + |
| 99 | + // eslint-disable-next-line no-console |
| 100 | + console.log(chalk.green('Running migration:'), migration); |
| 101 | + |
| 102 | + await jscodeshift.run(migrationFile, filepaths, { |
| 103 | + dry: flags.dry, |
| 104 | + print: flags.print, |
| 105 | + babel: true, |
| 106 | + ignorePattern: ['**/node_modules/**', '**/.next/**', '**/build/**'], |
| 107 | + extensions: 'tsx,ts,jsx,js', |
| 108 | + parser: 'tsx', |
| 109 | + verbose: 2, |
| 110 | + runInBand: true, |
| 111 | + silent: false, |
| 112 | + stdin: false, |
| 113 | + }); |
| 114 | + } catch (error) { |
| 115 | + // eslint-disable-next-line no-console |
| 116 | + console.error(error); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | +} |
0 commit comments