Skip to content

Commit

Permalink
refactor(generic): Use readJson and writeJson
Browse files Browse the repository at this point in the history
  • Loading branch information
malept committed Dec 18, 2017
1 parent 35120b1 commit 1a1884d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/api/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default async (providedOptions = {}) => {

const writeChanges = async () => {
await asyncOra('Writing modified package.json file', async () => {
await fs.writeFile(path.resolve(dir, 'package.json'), `${JSON.stringify(packageJSON, null, 2)}\n`);
await fs.writeJson(path.resolve(dir, 'package.json'), packageJSON, { spaces: 2 });
});
};

Expand Down Expand Up @@ -208,20 +208,20 @@ export default async (providedOptions = {}) => {
let babelConfig = packageJSON.babel;
const babelPath = path.resolve(dir, '.babelrc');
if (!babelConfig && await fs.pathExists(babelPath)) {
babelConfig = JSON.parse(await fs.readFile(babelPath, 'utf8'));
babelConfig = await fs.readJson(babelPath, 'utf8');
}

if (babelConfig) {
await asyncOra('Porting original babel config', async () => {
let compileConfig = {};
const compilePath = path.resolve(dir, '.compilerc');
if (await fs.pathExists(compilePath)) {
compileConfig = JSON.parse(await fs.readFile(compilePath, 'utf8'));
compileConfig = await fs.readJson(compilePath, 'utf8');
}

await fs.writeFile(compilePath, JSON.stringify(Object.assign(compileConfig, {
await fs.writeJson(compilePath, Object.assign(compileConfig, {
'application/javascript': babelConfig,
}), null, 2));
}), { spaces: 2 });
});

info(interactive, 'NOTE: You might be able to remove your `.compilerc` file completely if you are only using the `es2016` and `react` presets'.yellow);
Expand Down
2 changes: 1 addition & 1 deletion src/api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default async (providedOptions = {}) => {
if (copiedPackageJSON.config && copiedPackageJSON.config.forge) {
delete copiedPackageJSON.config.forge;
}
await fs.writeFile(path.resolve(buildPath, 'package.json'), JSON.stringify(copiedPackageJSON, null, 2));
await fs.writeJson(path.resolve(buildPath, 'package.json'), copiedPackageJSON, { spaces: 2 });
done();
});

Expand Down
6 changes: 3 additions & 3 deletions src/init/init-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default async (dir, lintStyle) => {
break;
}
d('writing package.json to:', dir);
await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON, null, 4));
await fs.writeJson(path.resolve(dir, 'package.json'), packageJSON, { spaces: 2 });
});

await asyncOra('Installing NPM Dependencies', async () => {
Expand Down Expand Up @@ -67,7 +67,7 @@ export default async (dir, lintStyle) => {

// NB: For babel-preset-env to work correctly, it needs to know the
// actual version of Electron that we installed
const content = JSON.parse(await fs.readFile(path.join(dir, '.compilerc'), 'utf8'));
const content = await fs.readJson(path.join(dir, '.compilerc'), 'utf8');
const electronPrebuilt = require(
path.join(dir, 'node_modules', 'electron-prebuilt-compile', 'package.json'));

Expand All @@ -78,6 +78,6 @@ export default async (dir, lintStyle) => {
envTarget[1].targets.electron = parseFloat(electronPrebuilt.version).toString();
}

await fs.writeFile(path.join(dir, '.compilerc'), JSON.stringify(content, null, 2), 'utf8');
await fs.writeJson(path.join(dir, '.compilerc'), content, { spaces: 2 });
});
};
4 changes: 1 addition & 3 deletions src/util/compile-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export default async(originalDir, buildPath, electronVersion, pPlatform, pArch,
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));
await fs.writeJson(path.join(appDir, 'package.json'), packageJSON, { spaces: 2 });
}

await compileAndShim(buildPath);
Expand Down
6 changes: 3 additions & 3 deletions src/util/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ class BasicConfigStore {
this._load();
this._store[key] = value;
d('setting key:', key, 'to value:', value);
fs.writeFileSync(this._path, JSON.stringify(this._store));
fs.writeJsonSync(this._path, this._store);
}

_load() {
if (fs.existsSync(this._path)) {
this._store = JSON.parse(fs.readFileSync(this._path, 'utf8'));
this._store = fs.readJsonSync(this._path);
}
}

reset() {
this._store = {};
fs.writeFileSync(this._path, JSON.stringify(this._store));
fs.writeJsonSync(this._path, this._store);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/util/read-package-json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import fs from 'fs-extra';
import path from 'path';

export default async (dir) => {
const packageData = await fs.readFile(path.resolve(dir, 'package.json'), 'utf8');
return JSON.parse(packageData);
};
export default async dir =>
await fs.readJson(path.resolve(dir, 'package.json'));
8 changes: 4 additions & 4 deletions test/slow/api_spec_slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => {

it('should have set the .compilerc electron version to be a string', async () => {
expectProjectPathExists('.compilerc', 'file');
const compilerc = JSON.parse(await fs.readFile(path.resolve(dir, '.compilerc')));
const compilerc = await fs.readJson(path.resolve(dir, '.compilerc'));
const electronVersion = compilerc.env.development['application/javascript'].presets[0][1].targets.electron;
expect(electronVersion).to.be.a('string');
expect(electronVersion.split('.').length).to.equal(2);
Expand Down Expand Up @@ -242,7 +242,7 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => {
}
packageJSON.homepage = 'http://www.example.com/';
packageJSON.author = 'Test Author';
await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON, null, 2));
await fs.writeJson(path.resolve(dir, 'package.json'), packageJSON);
});

it('can package to outDir without errors', async () => {
Expand All @@ -258,7 +258,7 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => {
it('can make from custom outDir without errors', async () => {
const packageJSON = await readPackageJSON(dir);
packageJSON.config.forge.make_targets[process.platform] = ['zip'];
await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON));
await fs.writeJson(path.resolve(dir, 'package.json'), packageJSON);

await forge.make({ dir, skipPackage: true, outDir: `${dir}/foo` });

Expand All @@ -276,7 +276,7 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => {
it('can package without errors', async () => {
const packageJSON = await readPackageJSON(dir);
packageJSON.config.forge.electronPackagerConfig.asar = true;
await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON, null, 2));
await fs.writeJson(path.resolve(dir, 'package.json'), packageJSON);

await forge.package({ dir });
});
Expand Down

0 comments on commit 1a1884d

Please sign in to comment.