I have a script which scaffolds an app. This involves making a new directory in the current directory inside which the app's scaffold files will be unpacked.
At the end of my script I run a series of npm/node commands as follows (appDir is a reference to the child dir created to host the app; it exists by the time these commands run):
const postInstCmds = [
'cd '+appDir,
'npm init -y',
'npm install add-npm-scripts --save-dev',
'npm install http-server --save-dev',
'add-npm-scripts server "http-server"',
'npm run server'
];
const execSync = require('child_process').execSync;
postInstCmds.forEach(cmd => execSync(cmd));
However it seems the first cd command is being ignored; the subsequent commands are being executed in the parent, not child, directory.
I've read this, which explicitly says that npm supports cd * operations, but perhaps I'm misunderstanding something (I'm no npm wizard). I've also tried using npm's --prefix argument to specify path, but no joy.
[UPDATE]
The linked dup suggests npm start <dir> but this seems to work only if <dir> already contains a package.json. As per my sequence of commands, I want to move to the directory and then create the package.json (via npm init). Another answer there suggests npm run --prefix, but run again requires an extant pacakge.json, since it reads from the scripts object.
I have also tried npm init --prefix <dir> but the arg doesn't seem to be supported for npm init.