Skip to content

Commit

Permalink
Add to package.json scripts (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
grabbou authored and satya164 committed Apr 5, 2017
1 parent 1b05eae commit 92ba8ba
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async function init() {
}

await addToXcodeBuild(cwd);
await addToPackageScripts(cwd);

progress = ora(messages.generatingConfig()).start();

Expand All @@ -146,6 +147,67 @@ async function init() {
const sleep = (time: number = 1000) =>
new Promise(resolve => setTimeout(resolve, time));

const getRunScript = (scriptName: string) => {
const runCommand = scriptName === 'start' ? 'yarn' : 'yarn run';
return `${runCommand} ${scriptName}`;
};

/**
* Adds Haul to package.json scripts
*/
const addToPackageScripts = async (cwd: string) => {
const pjson = JSON.parse(
fs.readFileSync(path.join(cwd, 'package.json')).toString(),
);

const scripts = pjson.scripts || {};

const haulScript = Object.keys(scripts).find(
name => scripts[name] === 'haul start',
);

if (haulScript) {
ora().info(
`Haul already exists in your package.json. Start Haul by running ${getRunScript(haulScript)}'`,
);
return;
}

let scriptName = 'start';

if (
scripts.start &&
scripts.start !== 'node ./node_modules/react-native/local-cli/cli.js start'
) {
const result = await inquirer.prompt([
{
type: 'input',
name: 'scriptName',
message: 'Enter the name of the script to add to package.json, e.g. `haul` for `yarn run haul`',
default: 'haul',
},
]);

scriptName = result.scriptName;
}

pjson.scripts = Object.assign({}, scripts, {
[scriptName]: 'haul start',
});

const progress = ora(
`Adding \`${scriptName}\` script to your package.json`,
).start();

await sleep();

fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(pjson));

progress.succeed(
`You can now start Haul by running '${getRunScript(scriptName)}'`,
);
};

/**
* Adds Haul to native iOS build pipeline
*/
Expand Down

0 comments on commit 92ba8ba

Please sign in to comment.