Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor setup: prompts, es6, escaping, helper #661

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 15 additions & 105 deletions tools/setup/setup.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,19 @@
/* eslint-disable no-var */
var rimraf = require('rimraf');
var chalk = require('chalk');
var replace = require("replace");
var prompt = require("prompt");
var prompts = require('./setupPrompts');
const prompts = require("prompts");
const questions = require('./setupPrompts');
const {
deleteGit,
updatePackage,
installSuccessMessage,
} = require('./setupHelper');

var chalkSuccess = chalk.green;
var chalkProcessing = chalk.blue;
var chalkWarn = chalk.red;
installSuccessMessage();

/* eslint-disable no-console */
(async () => {
const {shouldDeleteGit} = await prompts(questions.deleteGit)
const packageJson = await prompts(questions.packageJson);

console.log(chalkSuccess('Dependencies installed.'));
shouldDeleteGit ?
deleteGit(() => updatePackage(packageJson))
: updatePackage(packageJson);

prompt.start();

console.log(chalkWarn("WARNING: Preparing to delete local git repository..."));
prompt.get([{name: 'deleteGit', description: "Delete the git repository? [Y/n]"}], function(err, result) {
var deleteGit = result.deleteGit.toUpperCase();

if (err) {
process.exit(1);
}

function updatePackage() {
console.log(chalkProcessing('Updating package.json settings:'));

prompt.get(prompts, function(err, result) {
// parse user responses
// default values provided for fields that will cause npm to complain if left empty
const responses = [
{
key: 'name',
value: result.projectName || 'new-project'
},
{
key: 'version',
value: result.version || '0.1.0'
},
{
key: 'author',
value: result.author
},
{
key: 'license',
value: result.license || 'MIT'
},
{
key: 'description',
value: result.description
},
// simply use an empty URL here to clear the existing repo URL
{
key: 'url',
value: ''
}
];

// update package.json with the user's values
responses.forEach(res => {
replace({
regex: `("${res.key}"): "(.*?)"`,
replacement: `$1: "${res.value}"`,
paths: ['package.json'],
recursive: false,
silent: true
});
});

// reset package.json 'keywords' field to empty state
replace({
regex: /"keywords": \[[\s\S]+?\]/,
replacement: `"keywords": []`,
paths: ['package.json'],
recursive: false,
silent: true
});

// remove setup script from package.json
replace({
regex: /\s*"setup":.*,/,
replacement: "",
paths: ['package.json'],
recursive: false,
silent: true
});

// remove all setup scripts from the 'tools' folder
console.log(chalkSuccess('\nSetup complete! Cleaning up...\n'));
rimraf('./tools/setup', error => {
if (error) throw new Error(error);
});
});

}

if (deleteGit.match(/^N.*/)) {
updatePackage();
}
else {
// remove the original git repository
rimraf('.git', error => {
if (error) throw new Error(error);
console.log(chalkSuccess('Original Git repository removed.\n'));
updatePackage();
});
}
});
})();
65 changes: 65 additions & 0 deletions tools/setup/setupHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const rimraf = require('rimraf');
const replace = require("replace");
const chalk = require('chalk');

const chalkSuccess = chalk.green;
const chalkProcessing = chalk.blue;
const chalkWarn = chalk.red;

/* eslint-disable no-console */

module.exports = {

updatePackage(packageJson) {
console.log(chalkProcessing('Updating package.json settings:'));

// update package.json with the user's values
Object.entries(packageJson).forEach(([key, value]) => {
replace({
regex: `("${key}"): "(.*?)"`,
replacement: `$1: ${JSON.stringify(value)}`,
paths: ['package.json'],
recursive: false,
silent: true
});
});

// reset package.json 'keywords' field to empty state
replace({
regex: /"keywords": \[[\s\S]+?\]/,
replacement: `"keywords": []`,
paths: ['package.json'],
recursive: false,
silent: true,
});

// remove setup script from package.json
replace({
regex: /\s*"setup":.*,/,
replacement: "",
paths: ['package.json'],
recursive: false,
silent: true,
});

// remove all setup scripts from the 'tools' folder
console.log(chalkSuccess('\nSetup complete! Cleaning up...\n'));
rimraf('./tools/setup', error => {
if (error) throw new Error(error);
});
},

deleteGit(onDelete) {
console.log(chalkWarn("WARNING: Preparing to delete local git repository..."));

rimraf('.git', error => {
if (error) throw new Error(error);
console.log(chalkSuccess('Original Git repository removed.\n'));
onDelete();
});
},

installSuccessMessage() {
console.log(chalkSuccess('Dependencies installed.'));
},
};
1 change: 1 addition & 0 deletions tools/setup/setupMessage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This script displays an intro message for the setup script
/* eslint-disable no-console */

console.log('===========================');
console.log('= React Slingshot Setup =');
console.log('===========================\n');
Expand Down
88 changes: 63 additions & 25 deletions tools/setup/setupPrompts.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
// Define prompts for use with npm 'prompt' module in setup script
module.exports = [
{
name: 'projectName',
description: 'Project name (default: new-project)',
pattern: /^[^._][a-z0-9-_~]+$/,
message: 'Limited to: lowercase letters, numbers, period, hyphen, ' +
'underscore, and tilde; cannot begin with period or underscore.'
},
{
name: 'version',
description: 'Version (default: 0.1.0)'
},
{
name: 'author',
description: 'Author'
},
{
name: 'license',
description: 'License (default: MIT)'
},
{
name: 'description',
description: 'Project description'
// Define prompts for use with npm 'prompts' module in setup script

// exit "cleanly" on Ctrl+C https://github.com/terkelg/prompts/issues/252
const enableTerminalCursor = () => {
process.stdout.write('\x1B[?25h');
process.stdout.write('\n')
}
function onState({aborted}) {
if (aborted) {
enableTerminalCursor();
process.exit(1);
}
];
}


module.exports = {
deleteGit: {
type: 'toggle',
name: 'shouldDeleteGit',
message: 'Delete the git repository?',
initial: true,
active: 'yes',
inactive: 'no',
onState,
},
packageJson: [
{
type: 'text',
name: 'projectName',
message: 'Project name',
intial: 'new-project',
validate: (value) =>
!!value.match(/^[^._][a-z0-9-_~]+$/)
|| 'Limited to: lowercase letters, numbers, period, hyphen, underscore, and tilde; cannot begin with period or underscore.',
onState,
},
{
type: 'text',
name: 'version',
message: 'Version',
initial: '0.1.0',
onState,
},
{
type: 'text',
name: 'author',
message: 'Author',
onState,
},
{
type: 'text',
name: 'license',
message: 'License',
initial: 'MIT',
onState,
},
{
type: 'text',
name: 'description',
message: 'Project description',
onState,
},
],
};