#!/usr/bin/env node
'use strict';
const {exec} = require('child-process-promise');
const {existsSync} = require('fs');
const {join} = require('path');
const {execRead, logPromise} = require('../utils');
const theme = require('../theme');
const run = async ({cwd, local, packages, version}) => {
if (local) {
if (!existsSync(join(cwd, 'build', 'node_modules', 'react'))) {
console.error(theme.error`No local build exists.`);
process.exit(1);
}
return;
}
if (!existsSync(join(cwd, 'build'))) {
await exec(`mkdir ./build`, {cwd});
}
await exec(`rm -rf ./build/node_modules*`, {cwd});
await exec(`mkdir ./build/node_modules`, {cwd});
const nodeModulesPath = join(cwd, 'build/node_modules');
for (let i = 0; i < packages.length; i++) {
const packageName = packages[i];
const url = await execRead(
`npm view ${packageName}@${version} dist.tarball`
);
const filePath = join(nodeModulesPath, `${packageName}.tgz`);
const packagePath = join(nodeModulesPath, `${packageName}`);
const tempPackagePath = join(nodeModulesPath, 'package');
await exec(`curl -L ${url} > ${filePath}`, {cwd});
await exec(`tar -xvzf ${filePath} -C ${nodeModulesPath}`, {cwd});
await exec(`mv ${tempPackagePath} ${packagePath}`, {cwd});
await exec(`rm ${filePath}`, {cwd});
}
};
module.exports = async params => {
return logPromise(
run(params),
theme`Checking out "next" from NPM {version ${params.version}}`
);
};