feat(importer): create inital import logic

This commit is contained in:
Samuel Attard 2016-12-04 19:55:10 +11:00 коммит произвёл Mark Lee
Родитель 3b01f08c31
Коммит bddb903841
6 изменённых файлов: 189 добавлений и 9 удалений

Просмотреть файл

@ -0,0 +1,143 @@
import debug from 'debug';
import fs from 'fs-promise';
import inquirer from 'inquirer';
import ora from 'ora';
import path from 'path';
import pify from 'pify';
import program from 'commander';
import rimraf from 'rimraf';
import { spawn as yarnOrNPMSpawn, hasYarn } from 'yarn-or-npm';
import initGit from './init/init-git';
import { deps, devDeps } from './init/init-npm';
import installDepList from './util/install-dependencies';
import './util/terminate';
const d = debug('electron-forge:import');
const main = async () => {
let dir = process.cwd();
program
.version(require('../package.json').version)
.arguments('[name]')
.action((name) => {
if (!name) return;
if (path.isAbsolute(name)) {
dir = name;
} else {
dir = path.resolve(dir, name);
}
})
.parse(process.argv);
d(`Attempting to import project in: ${dir}`);
if (!await fs.exists(dir) || !await fs.exists(path.resolve(dir, 'package.json'))) {
console.error(`We couldn't find a project in: ${dir}`.red);
process.exit(1);
}
const { confirm } = await inquirer.createPromptModule()({
type: 'confirm',
name: 'confirm',
message: `WARNING: We will now attempt to import: "${dir}". This will involve modifying some files, are you sure you want to continue?`,
});
if (!confirm) {
process.exit(1);
}
await initGit(dir);
const packageJSONPath = path.resolve(dir, 'package.json');
let packageJSON = JSON.parse(await fs.readFile(packageJSONPath, 'utf8'));
if (packageJSON.config && packageJSON.config.forge) {
console.warn('It looks like this project is already configured for "electron-forge"'.green);
const { shouldContinue } = await inquirer.createPromptModule()({
type: 'confirm',
name: 'shouldContinue',
message: 'Are you sure you want to continue?',
});
if (!shouldContinue) {
process.exit(0);
}
}
const { shouldChangeMain } = await inquirer.createPromptModule()({
type: 'confirm',
name: 'shouldChangeMain',
message: 'Do you want us to change the "main" attribute of your package.json? If you are currently using babel and pointint to a "build" directory say yes.', // eslint-disable-line
});
if (shouldChangeMain) {
const { newMain } = await inquirer.createPromptModule()({
type: 'input',
name: 'newMain',
default: packageJSON.main,
message: 'Enter the relative path to your uncompiled main file',
});
packageJSON.main = newMain;
}
packageJSON.dependencies = packageJSON.dependencies || {};
packageJSON.devDependencies = packageJSON.devDependencies || {};
const keys = Object.keys(packageJSON.dependencies).concat(Object.keys(packageJSON.devDependencies));
let electronName;
for (const key of keys) {
if (key === 'electron' || key === 'electron-prebuilt') {
delete packageJSON.dependencies[key];
delete packageJSON.devDependencies[key];
electronName = key;
}
}
const writeChanges = async () => {
const writeSpinner = ora.ora('Writing modified package.json file').start();
await fs.writeFile(packageJSONPath, `${JSON.stringify(packageJSON, null, 2)}\n`);
writeSpinner.succeed();
};
let electronVersion;
if (electronName) {
electronVersion = JSON.parse(await fs.readFile(path.resolve(dir, 'node_modules', electronName, 'package.json'))).version;
packageJSON.devDependencies['electron-prebuilt-compile'] = electronVersion;
}
await writeChanges();
if (electronName) {
const pruneSpinner = ora.ora('Pruning deleted modules').start();
await new Promise((resolve) => {
d('attempting to prune node_modules in:', dir);
const child = yarnOrNPMSpawn(hasYarn() ? [] : ['prune'], {
cwd: dir,
stdio: 'ignore',
});
child.on('exit', () => resolve());
});
pruneSpinner.succeed();
const installSpinner = ora.ora('Installing dependencies').start();
await pify(rimraf)(path.resolve(dir, 'node_modules/.bin/electron'));
await pify(rimraf)(path.resolve(dir, 'node_modules/.bin/electron.cmd'));
await pify(rimraf)(path.resolve(dir, 'node_modules', electronName));
d('installing dependencies');
await installDepList(dir, deps);
d('installing devDependencies');
await installDepList(dir, devDeps, true);
d('installing electron-prebuilt-compile');
await installDepList(dir, [`electron-prebuilt-compile@${electronVersion}`], false, true);
installSpinner.succeed();
}
packageJSON = JSON.parse(await fs.readFile(packageJSONPath, 'utf8'));
packageJSON.config = packageJSON.config || {};
packageJSON.config.forge = JSON.parse(await fs.readFile(path.resolve(__dirname, '../tmpl/package.json'))).config.forge;
await writeChanges();
};
main();

Просмотреть файл

@ -25,7 +25,7 @@ const main = async () => {
dir = path.resolve(dir, cwd);
}
})
.parse(process.argv);
.parse(process.argv.slice(0, 2));
dir = await resolveDir(dir);
if (!dir) {

Просмотреть файл

@ -21,6 +21,7 @@ checkSystem()
.version(require('../package.json').version)
.option('--verbose', 'Enables verbose mode')
.command('init', 'Initialize a new Electron application')
.command('import', 'Attempts to navigate you through the process of importing an existing project to "electron-forge"')
.command('lint', 'Lints the current Electron application')
.command('package', 'Package the current Electron application')
.command('make', 'Generate distributables for the current Electron application')

Просмотреть файл

@ -1,12 +1,19 @@
import { exec } from 'child_process';
import debug from 'debug';
import fs from 'fs-promise';
import ora from 'ora';
import path from 'path';
const d = debug('electron-forge:init:git');
export default async dir =>
new Promise((resolve, reject) => {
new Promise(async (resolve, reject) => {
const spinner = ora.ora('Initializing Git Repository').start();
if (await fs.exists(path.resolve(dir, '.git'))) {
d('.git directory already exists, skipping git initialization');
spinner.succeed();
return resolve();
}
d('executing "git init" in directory:', dir);
exec('git init', {
cwd: dir,

Просмотреть файл

@ -9,11 +9,11 @@ import readPackageJSON from '../util/read-package-json';
const d = debug('electron-forge:init:npm');
const deps = ['electron-compile'];
const devDeps = ['babel-preset-stage-0'];
const exactDevDeps = ['electron-prebuilt-compile'];
const standardDeps = ['standard'];
const airbnDeps = ['eslint', 'eslint-config-airbnb', 'eslint-plugin-import',
export const deps = ['electron-compile'];
export const devDeps = ['babel-preset-stage-0'];
export const exactDevDeps = ['electron-prebuilt-compile'];
export const standardDeps = ['standard'];
export const airbnDeps = ['eslint', 'eslint-config-airbnb', 'eslint-plugin-import',
'eslint-plugin-jsx-a11y@^2.2.3', 'eslint-plugin-react'];
export default async (dir, lintStyle) => {
@ -27,9 +27,11 @@ export default async (dir, lintStyle) => {
packageJSON.scripts.lint = 'standard';
break;
case 'airbnb':
default:
packageJSON.scripts.lint = 'eslint src';
break;
default:
packageJSON.scripts.lint = 'echo "No linting yet..."';
break;
}
d('writing package.json to:', dir);
await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON, null, 4));

Просмотреть файл

@ -4,13 +4,17 @@ import ora from 'ora';
const d = debug('electron-forge:lifecycle');
const runningOras = {};
process.on('unhandledRejection', (err) => {
Object.keys(runningOras).forEach(key => runningOras[key].fail());
process.stdout.write('\n\nAn unhandled rejection has occurred inside Forge:\n');
console.error(colors.red(err.stack || JSON.stringify(err)));
process.exit(1);
});
process.on('uncaughtException', (err) => {
Object.keys(runningOras).forEach(key => runningOras[key].fail());
process.stdout.write('\n\nAn unhandled exception has occurred inside Forge:\n');
console.error(colors.red(err.stack || JSON.stringify(err)));
process.exit(1);
@ -40,5 +44,28 @@ if (process.env.DEBUG && process.env.DEBUG.includes('electron-forge')) {
return fake;
};
} else {
ora.ora = ora;
let oraID = 1;
ora.ora = (name) => {
const createdOra = ora(name);
createdOra.id = oraID;
const fns = {};
fns.start = createdOra.start.bind(createdOra);
fns.stop = createdOra.stop.bind(createdOra);
fns.succeed = createdOra.succeed.bind(createdOra);
fns.fail = createdOra.fail.bind(createdOra);
createdOra.start = () => {
runningOras[createdOra.id] = createdOra;
fns.start();
return createdOra;
};
['stop', 'succeed', 'fail'].forEach((fnName) => {
createdOra[fnName] = (...args) => {
delete runningOras[createdOra.id];
fns[fnName](...args);
return createdOra;
};
});
oraID += 1;
return createdOra;
};
}