Skip to content

Commit

Permalink
add -w in package scripts codemods
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Jul 19, 2024
1 parent 40e85c3 commit 6186a85
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/four-penguins-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@dmno/nextjs-integration": patch
"dmno": patch
---

add watch mode to package scripts codemods
17 changes: 12 additions & 5 deletions packages/core/src/cli/lib/init-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,25 @@ export async function initDmnoForService(workspaceInfo: ScannedWorkspaceInfo, se
// run package.json script codemods
const packageScriptsCodemods = integrationMeta.packageScriptsCodemods;
if (packageScriptsCodemods?.prependDmnoRun) {
const prependScripts = packageScriptsCodemods.prependDmnoRun as Record<string, string>;
const prependScripts: Record<string, {
command: string, args?: string
}> = packageScriptsCodemods.prependDmnoRun;
const packageJsonPath = `${service.path}/package.json`;
const packageJsonStr = await fs.promises.readFile(packageJsonPath, 'utf8');
const packageJson = parseJSONC(packageJsonStr);

const packageJsonEdits = [] as Array<any>;
for (const command in prependScripts) {
const existingScriptCmd = packageJson.scripts[command];
for (const commandName in prependScripts) {
const existingScriptCmd = packageJson.scripts[commandName];
if (!existingScriptCmd) continue;
if (!existingScriptCmd.includes('dmno run')) {
const prependedScript = packageJson.scripts[command].replace(prependScripts[command], `dmno run -- ${prependScripts[command]}`);
packageJsonEdits.push(...modifyJSONC(packageJsonStr, ['scripts', command], prependedScript, {}));
let argsStr = prependScripts[commandName].args || '';
if (argsStr && !argsStr.endsWith(' ')) argsStr += ' ';
const prependedScript = packageJson.scripts[commandName].replace(
prependScripts[commandName].command,
`dmno run ${argsStr}-- ${prependScripts[commandName].command}`,
);
packageJsonEdits.push(...modifyJSONC(packageJsonStr, ['scripts', commandName], prependedScript, {}));
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/integrations/nextjs/dmno.meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"action": {
"wrapWithFn": "dmnoNextConfigPlugin()",
},
} ],
} ]
}
],
"packageScriptsCodemods": {
"prependDmnoRun": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"dev": { "command": "next dev", "args": "-w" },
"build": { "command": "next build" },
"start": { "command": "next start" },
"lint": { "command": "next lint" }
}
},
}

0 comments on commit 6186a85

Please # to comment.