I'm trying to execute some shell commands synchronously to install npm dependencies, build packages and create a database in docker.
['api', 'front-end'].forEach(async (dir) => {
await new Promise((resolve, reject) => {
console.log(`Installing npm dependencies for ${dir}`);
exec('npm install', { cwd: path.join(initDir, 'pushkin', dir) }, (err) => {
if (err) console.error(`Failed to install npm dependencies for ${dir}: ${err}`);
if (dir !== 'api' && dir !== 'front-end') return;
});
resolve(`${dir} installed...`);
})
.then(() => {
console.log(`Building ${dir}`);
exec('npm run build', { cwd: path.join(process.cwd(), 'pushkin', dir) }, (err) => {
if (err) console.error(`Failed to build ${dir}: ${err}`);
console.log(`${dir} is built`);
});
})
.then(() => {
shell.exec(startDbCommand);
})
.then(() => {
shell.exec(createDbCommand);
})
.then(() => {
shell.exec(stopDbCommand);
});
});
The docker commands are:
const startDbCommand = 'docker-compose -f pushkin/docker-compose.dev.yml up --no-start && docker-compose -f pushkin/docker-compose.dev.yml start test_db';
const createDbCommand = 'docker-compose -f pushkin/docker-compose.dev.yml exec -T test_db psql -U postgres -c "create database test_db"';
const stopDbCommand = 'docker-compose -f pushkin/docker-compose.dev.yml stop test_db';
When I ran it for the first time, I got this error:
No container found for test_db_1
Failed to build front-end: Error: Command failed: npm run build
sh: react-scripts: command not found
Failed to build api: Error: Command failed: npm run build
sh: babel: command not found
However, after I ran it again for the second time, everything seems to be fine. Is this a problem about the Promise chain I wrote? Thanks.
execSync