Skip to content

Commit

Permalink
Update @shopify/polaris-migrator to accept a file list via stdin (#…
Browse files Browse the repository at this point in the history
…10950)

Updates the Polaris migrator CLI to accept a file list via `stdin`.

Example usage with redirects:

```sh
npx @shopify/polaris-migrator styles-replace-custom-property \
  --decl=color \
  --from=--p-text \
  --to=--p-color-text \
  --stdin < file-list.txt
```

Example usage with piping:

```sh
git diff --name-only | npx @shopify/polaris-migrator styles-replace-custom-property \
  --decl=color \
  --from=--p-text \
  --to=--p-color-text \
  --stdin
```
  • Loading branch information
aaronccasanova authored Oct 11, 2023
1 parent d0f75e6 commit da2466f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-llamas-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris-migrator': minor
---

Updated the CLI to accept a file list via `stdin`
25 changes: 24 additions & 1 deletion polaris-migrator/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export const cliConfig = createCLIConfig({
description: 'Bypass Git safety checks and forcibly run migrations',
type: 'boolean',
},
stdin: {
alias: 's',
type: 'boolean',
description: 'If true, each line of the standard input is used as a path',
},
},
});

Expand Down Expand Up @@ -89,5 +94,23 @@ const {input, flags} = meow({
});

export async function run() {
await migrate(input[0], input[1], flags);
let files: string | string[];

if (flags.stdin) {
let buffer = '';

for await (const chunk of process.stdin) {
buffer += chunk.toString();
}

files = buffer.split('\n').filter(Boolean);

if (files.length === 0) {
throw new Error('No files provided via stdin');
}
} else {
files = input[1];
}

await migrate(input[0], files, flags);
}
7 changes: 5 additions & 2 deletions polaris-migrator/src/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export interface MigrateOptions {
dry?: boolean;
print?: boolean;
force?: boolean;
stdin?: boolean;
}

export async function migrate(
migration: string,
files: string,
files: string | string[],
options: MigrateOptions = {},
) {
const migrationFile = path.join(
Expand All @@ -28,7 +29,9 @@ export async function migrate(
throw new Error(`No migration found for ${migration}`);
}

if (!files) throw new Error(`No path provided for migration`);
if (!options.stdin && !files) {
throw new Error(`No path provided for migration`);
}

if (!options.dry) {
checkGitStatus(options.force);
Expand Down

0 comments on commit da2466f

Please sign in to comment.