Skip to content

Commit

Permalink
Move steps into their own functions
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Jul 15, 2019
1 parent c29690e commit 925c537
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,59 @@ if (process.env.NOYARNPOSTINSTALL) {

process.chdir(path.resolve(__dirname, '..'));

console.log('Syncing workspace packages');
execSync('yarn syncpack fix-mismatches -p -d -P');
function syncWorkspace() {
console.log('Syncing workspaces');
execSync('yarn syncpack fix-mismatches -p -d -P');
}

const lockFile = path.resolve(__dirname, '../yarn.lock');
const yarnlock = fs.readFileSync(lockFile, 'utf8');
function deduplicatePackages() {
const lockFile = path.resolve(__dirname, '../yarn.lock');
const yarnlock = fs.readFileSync(lockFile, 'utf8');

console.log('Checking for duplicated packages');
const duplicates = deduplicate.listDuplicates(yarnlock);
if (duplicates.length === 0) {
console.log('No duplicated packages found');
process.exit(0);
}
console.log('Deduplicating packages');
const duplicates = deduplicate.listDuplicates(yarnlock);
if (duplicates.length === 0) {
console.log(' No duplicated packages found');
return;
}

console.log(
[
'',
`${duplicates.length} duplicate package(s) found:`,
duplicates.map(x => ` ${x}`).join('\n'),
'',
].join('\n'),
);

if (process.env.CI) {
console.error('There are duplicated packages, make sure to deduplicate by running yarn install');
process.exit(1);
}
console.log(
[
` ${duplicates.length} duplicated package(s) found:`,
duplicates.map(x => ` ${x}`).join('\n'),
].join('\n'),
);

console.log('Deduplicating...');
fs.writeFileSync(lockFile, deduplicate.fixDuplicates(yarnlock));
if (process.env.CI) {
console.error(
'Error: There are duplicated packages, make sure to deduplicate by running yarn install',
);
process.exit(1);
}

console.log('Installing...');
const yarn = spawn('yarn cross-env NOYARNPOSTINSTALL=1 yarn install', { shell: true });
yarn.stdout.on('data', data => {
console.log(` ${data.toString().trimEnd()}`);
});
fs.writeFileSync(lockFile, deduplicate.fixDuplicates(yarnlock));

yarn.stderr.on('data', data => {
console.log(` ${data.toString().trimEnd()}`);
});
console.log('Installing packages');
const yarn = spawn('yarn cross-env NOYARNPOSTINSTALL=1 yarn install', { shell: true });
yarn.stdout.on('data', data => {
console.log(` ${data.toString().trimEnd()}`);
});

yarn.on('close', code => {
if (code === 0) {
console.log('Success');
return;
}
yarn.stderr.on('data', data => {
console.log(` ${data.toString().trimEnd()}`);
});

yarn.on('close', code => {
if (code === 0) {
console.log(' Success');
return;
}

process.exit(code);
});
}

syncWorkspace();
deduplicatePackages();

console.error(`Error: child process exited with code ${code}`);
});
console.log('Done');

0 comments on commit 925c537

Please sign in to comment.