Skip to content
Permalink
Browse files

refactor(generic): standardize reading package.json files (#33)

  • Loading branch information
malept committed Dec 18, 2016
1 parent 202987e commit 0855eacf8ac2f36e74944e20ae1004598e4028eb
@@ -8,6 +8,7 @@ import './util/terminate';
import electronHostArch from './util/electron-host-arch';
import getForgeConfig from './util/forge-config';
import packager from './electron-forge-package';
import readPackageJSON from './util/read-package-json';
import resolveDir from './util/resolve-dir';

const main = async () => {
@@ -75,7 +76,7 @@ const main = async () => {
}
}

const packageJSON = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'), 'utf8'));
const packageJSON = await readPackageJSON(dir);
const appName = packageJSON.productName || packageJSON.name;

for (const targetArch of targetArchs) {
@@ -10,6 +10,7 @@ import './util/terminate';
import electronHostArch from './util/electron-host-arch';
import getForgeConfig from './util/forge-config';
import packagerCompileHook from './util/compile-hook';
import readPackageJSON from './util/read-package-json';
import rebuildHook from './util/rebuild';
import resolveDir from './util/resolve-dir';

@@ -48,7 +49,7 @@ const main = async () => {
process.exit(1);
}

const packageJSON = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'), 'utf8'));
const packageJSON = await readPackageJSON(dir);

const forgeConfig = await getForgeConfig(dir);
let packagerSpinner;
@@ -6,6 +6,7 @@ import program from 'commander';
import ora from 'ora';

import './util/terminate';
import readPackageJSON from './util/read-package-json';
import rebuild from './util/rebuild';
import resolveDir from './util/resolve-dir';

@@ -35,7 +36,7 @@ const main = async () => {
}
locateSpinner.succeed();

const packageJSON = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'), 'utf8'));
const packageJSON = await readPackageJSON(dir);

await rebuild(dir, packageJSON.devDependencies['electron-prebuilt-compile'], process.platform, process.arch);

@@ -5,6 +5,7 @@ import path from 'path';
import username from 'username';

import installDepList from '../util/install-dependencies';
import readPackageJSON from '../util/read-package-json';

const d = debug('electron-forge:init:npm');

@@ -17,7 +18,7 @@ const airbnDeps = ['eslint', 'eslint-config-airbnb', 'eslint-plugin-import',

export default async (dir, lintStyle) => {
const initSpinner = ora.ora('Initializing NPM Module').start();
const packageJSON = JSON.parse(await fs.readFile(path.resolve(__dirname, '../../tmpl/package.json'), 'utf8'));
const packageJSON = await readPackageJSON(path.resolve(__dirname, '../../tmpl'));
packageJSON.productName = packageJSON.name = path.basename(dir).toLowerCase();
packageJSON.config.forge.electronWinstallerConfig.name = packageJSON.name.replace(/-/g, '_');
packageJSON.author = await username();
@@ -1,6 +1,7 @@
import fs from 'fs-promise';
import ora from 'ora';
import path from 'path';
import readPackageJSON from './read-package-json';

export default async(originalDir, buildPath, electronVersion, pPlatform, pArch, done) => {
const compileSpinner = ora.ora('Compiling Application').start();
@@ -21,18 +22,18 @@ export default async(originalDir, buildPath, electronVersion, pPlatform, pArch,
}
}

const packageJson = JSON.parse(await fs.readFile(path.join(appDir, 'package.json'), 'utf8'));
const packageJSON = await readPackageJSON(appDir);

const index = packageJson.main || 'index.js';
packageJson.originalMain = index;
packageJson.main = 'es6-shim.js';
const index = packageJSON.main || 'index.js';
packageJSON.originalMain = index;
packageJSON.main = 'es6-shim.js';

await fs.writeFile(path.join(appDir, 'es6-shim.js'),
await fs.readFile(path.join(path.resolve(originalDir, 'node_modules/electron-compile/lib/es6-shim.js')), 'utf8'));

await fs.writeFile(
path.join(appDir, 'package.json'),
JSON.stringify(packageJson, null, 2));
JSON.stringify(packageJSON, null, 2));
}

await compileAndShim(buildPath);
@@ -1,8 +1,9 @@
import fs from 'fs-promise';
import path from 'path';
import readPackageJSON from './read-package-json';

export default async (dir) => {
let forgeConfig = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'))).config.forge;
let forgeConfig = (await readPackageJSON(dir)).config.forge;
if (typeof forgeConfig === 'string' && (await fs.exists(path.resolve(dir, forgeConfig)) || await fs.exists(path.resolve(dir, `${forgeConfig}.js`)))) {
try {
forgeConfig = require(path.resolve(dir, forgeConfig));
@@ -0,0 +1,7 @@
import fs from 'fs-promise';
import path from 'path';

export default async (dir) => {
const packageData = await fs.readFile(path.resolve(dir, 'package.json'), 'utf8');
return JSON.parse(packageData);
};
@@ -4,6 +4,7 @@ import fs from 'fs-promise';
import ora from 'ora';
import os from 'os';
import path from 'path';
import readPackageJSON from './read-package-json';

const d = debug('electron-forge:rebuild');

@@ -41,7 +42,7 @@ export default async (buildPath, electronVersion, pPlatform, pArch) => {
'--build-from-source',
];

const modulePackageJSON = JSON.parse(await fs.readFile(path.resolve(modulePath, 'package.json')));
const modulePackageJSON = await readPackageJSON(modulePath);
Object.keys(modulePackageJSON.binary || {}).forEach((binaryKey) => {
let value = modulePackageJSON.binary[binaryKey];
if (binaryKey === 'module_path') {
@@ -109,7 +110,7 @@ export default async (buildPath, electronVersion, pPlatform, pArch) => {

const markChildrenAsProdDeps = async (modulePath) => {
d('exporing:', modulePath);
const childPackageJSON = JSON.parse(await fs.readFile(path.resolve(modulePath, 'package.json'), 'utf8'));
const childPackageJSON = await readPackageJSON(modulePath);
const moduleWait = [];
Object.keys(childPackageJSON.dependencies || {}).forEach((key) => {
if (prodDeps[key]) return;
@@ -119,7 +120,7 @@ export default async (buildPath, electronVersion, pPlatform, pArch) => {
await Promise.all(moduleWait);
};

const rootPackageJSON = JSON.parse(await fs.readFile(path.resolve(buildPath, 'package.json'), 'utf8'));
const rootPackageJSON = await readPackageJSON(buildPath);
const markWaiters = [];
Object.keys(rootPackageJSON.dependencies || {}).concat(Object.keys(rootPackageJSON.optionalDependencies || {})).forEach((key) => {
prodDeps[key] = true;
@@ -1,6 +1,7 @@
import debug from 'debug';
import fs from 'fs-promise';
import path from 'path';
import readPackageJSON from './read-package-json';

const d = debug('electron-forge:project-resolver');

@@ -12,7 +13,7 @@ export default async (dir) => {
const testPath = path.resolve(mDir, 'package.json');
d('searching for project in:', mDir);
if (await fs.exists(testPath)) {
const packageJSON = JSON.parse(await fs.readFile(testPath, 'utf8'));
const packageJSON = await readPackageJSON(mDir);

if (packageJSON.devDependencies && packageJSON.devDependencies['electron-prebuilt-compile']) {
if (!/[0-9]/.test(packageJSON.devDependencies['electron-prebuilt-compile'][0])) {
@@ -6,6 +6,7 @@ import path from 'path';
import { expect } from 'chai';

import installDeps from '../src/util/install-dependencies';
import readPackageJSON from '../src/util/read-package-json';

const pSpawn = async (args = [], opts = {
stdio: process.platform === 'win32' ? 'inherit' : 'pipe',
@@ -103,7 +104,7 @@ describe(`electron-forge CLI (with installer=${installer.substr(12)})`, () => {
[].concat(targets).concat(genericTargets).forEach((target) => {
describe(`make (with target=${target})`, () => {
before(async () => {
const packageJSON = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'), 'utf8'));
const packageJSON = await readPackageJSON(dir);
packageJSON.config.forge.make_targets[process.platform] = [target];
await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON));
});

0 comments on commit 0855eac

Please sign in to comment.
You can’t perform that action at this time.