Skip to content
Permalink
Browse files

refactor(generic): replace fs-promise with fs-extra

As of fs-extra 3.0.0, it comes with builtin Promise support,
so a wrapper module is no longer necessary.

Also comes with a non-deprecated fs.exists method.
  • Loading branch information
malept committed May 2, 2017
1 parent 855c487 commit 012b152f94b04c4f4ba2aa27b23b572a846cd4b6
@@ -111,7 +111,7 @@
"electron-rebuild": "^1.5.7",
"electron-sudo": "malept/electron-sudo#fix-linux-sudo-detection",
"form-data": "^2.1.4",
"fs-promise": "^2.0.0",
"fs-extra": "^3.0.0",
"github": "^9.0.0",
"glob": "^7.1.1",
"inquirer": "^3.0.1 ",
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import inquirer from 'inquirer';
import path from 'path';
import { spawn as yarnOrNPMSpawn, hasYarn } from 'yarn-or-npm';
@@ -44,7 +44,7 @@ export default async (providedOptions = {}) => {
asyncOra.interactive = interactive;

d(`Attempting to import project in: ${dir}`);
if (!await fs.exists(dir) || !await fs.exists(path.resolve(dir, 'package.json'))) {
if (!await fs.pathExists(dir) || !await fs.pathExists(path.resolve(dir, 'package.json'))) {
throw `We couldn't find a project in: ${dir}`;
}

@@ -194,7 +194,7 @@ export default async (providedOptions = {}) => {
await writeChanges();

await asyncOra('Fixing .gitignore', async () => {
if (await fs.exists(path.resolve(dir, '.gitignore'))) {
if (await fs.pathExists(path.resolve(dir, '.gitignore'))) {
const gitignore = await fs.readFile(path.resolve(dir, '.gitignore'));
if (!gitignore.includes(outDir)) {
await fs.writeFile(path.resolve(dir, '.gitignore'), `${gitignore}\n${outDir}/`);
@@ -204,15 +204,15 @@ export default async (providedOptions = {}) => {

let babelConfig = packageJSON.babel;
const babelPath = path.resolve(dir, '.babelrc');
if (!babelConfig && await fs.exists(babelPath)) {
if (!babelConfig && await fs.pathExists(babelPath)) {
babelConfig = JSON.parse(await fs.readFile(babelPath, 'utf8'));
}

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

@@ -1,7 +1,7 @@
import 'colors';
import debug from 'debug';
import fetch from 'node-fetch';
import fs from 'fs-promise';
import fs from 'fs-extra';
import inquirer from 'inquirer';
import nugget from 'nugget';
import os from 'os';
@@ -130,7 +130,7 @@ export default async (providedOptions = {}) => {
const filename = `${pathSafeRepo}-${latestRelease.tag_name}-${targetAsset.name}`;

const fullFilePath = path.resolve(tmpdir, filename);
if (!await fs.exists(fullFilePath) || (await fs.stat(fullFilePath)).size !== targetAsset.size) {
if (!await fs.pathExists(fullFilePath) || (await fs.stat(fullFilePath)).size !== targetAsset.size) {
await fs.mkdirs(tmpdir);

const nuggetOpts = {
@@ -1,5 +1,5 @@
import 'colors';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

import asyncOra from '../util/ora-handler';
@@ -136,7 +136,7 @@ export default async (providedOptions = {}) => {

for (const targetArch of targetArchs) {
const packageDir = path.resolve(outDir, `${appName}-${platform}-${targetArch}`);
if (!(await fs.exists(packageDir))) {
if (!(await fs.pathExists(packageDir))) {
throw new Error(`Couldn't find packaged app at: ${packageDir}`);
}

@@ -1,6 +1,6 @@
import 'colors';
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import glob from 'glob';
import path from 'path';
import pify from 'pify';
@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import program from 'commander';

@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import program from 'commander';

@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import program from 'commander';

@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import program from 'commander';

@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import glob from 'glob';
import resolvePackage from 'resolve-package';
import path from 'path';
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import logSymbols from 'log-symbols';

import asyncOra from '../util/ora-handler';
@@ -1,6 +1,6 @@
import { exec } from 'child_process';
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

import asyncOra from '../util/ora-handler';
@@ -10,7 +10,7 @@ const d = debug('electron-forge:init:git');
export default async (dir) => {
await asyncOra('Initializing Git Repository', async () => {
await new Promise(async (resolve, reject) => {
if (await fs.exists(path.resolve(dir, '.git'))) {
if (await fs.pathExists(path.resolve(dir, '.git'))) {
d('.git directory already exists, skipping git initialization');
return resolve();
}
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import username from 'username';

@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

import asyncOra from '../util/ora-handler';
@@ -1,5 +1,5 @@
import { spawn } from 'child_process';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

import { getMountedImages, mountImage, unmountImage } from '../../util/hdiutil';
@@ -1,5 +1,5 @@
import { spawn } from 'child_process';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import moveApp from '../../util/move-app';

@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

import { ensureDirectory } from '../../util/ensure-output';
@@ -31,11 +31,11 @@ export default async ({ dir, appName, targetArch, forgeConfig, packageJSON }) =>
path.resolve(outPath, `${winstallerConfig.name}-${packageJSON.version}-full.nupkg`),
];
const deltaPath = path.resolve(outPath, `${winstallerConfig.name}-${packageJSON.version}-delta.nupkg`);
if (winstallerConfig.remoteReleases || await fs.exists(deltaPath)) {
if (winstallerConfig.remoteReleases || await fs.pathExists(deltaPath)) {
artifacts.push(deltaPath);
}
const msiPath = path.resolve(outPath, winstallerConfig.setupMsi || `${appName}Setup.msi`);
if (!winstallerConfig.noMsi && await fs.exists(msiPath)) {
if (!winstallerConfig.noMsi && await fs.pathExists(msiPath)) {
artifacts.push(msiPath);
}
return artifacts;
@@ -1,7 +1,7 @@
import debug from 'debug';
import fetch from 'node-fetch';
import FormData from 'form-data';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

import asyncOra from '../util/ora-handler';
@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

import asyncOra from './ora-handler';
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';

@@ -1,10 +1,10 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

// This is different from fs-extra's ensureDir because it wipes out the existing directory,
// if it's found.
async function ensureDirectory(dir) {
if (await fs.exists(dir)) {
if (await fs.pathExists(dir)) {
await fs.remove(dir);
}
return fs.mkdirs(dir);
@@ -13,7 +13,7 @@ async function ensureDirectory(dir) {
// This is different from fs-extra's ensureFile because it wipes out the existing file,
// if it's found.
async function ensureFile(file) {
if (await fs.exists(file)) {
if (await fs.pathExists(file)) {
await fs.remove(file);
}
await fs.mkdirs(path.dirname(file));
@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import _template from 'lodash.template';
import readPackageJSON from './read-package-json';
@@ -30,7 +30,7 @@ const proxify = (object, envPrefix) => {
export default async (dir) => {
const packageJSON = await readPackageJSON(dir);
let forgeConfig = packageJSON.config.forge;
if (typeof forgeConfig === 'string' && (await fs.exists(path.resolve(dir, forgeConfig)) || await fs.exists(path.resolve(dir, `${forgeConfig}.js`)))) {
if (typeof forgeConfig === 'string' && (await fs.pathExists(path.resolve(dir, forgeConfig)) || await fs.pathExists(path.resolve(dir, `${forgeConfig}.js`)))) {
try {
forgeConfig = require(path.resolve(dir, forgeConfig));
} catch (err) {
@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import inquirer from 'inquirer';
import path from 'path';
import pify from 'pify';
@@ -13,7 +13,7 @@ export default async (appPath, targetApplicationPath, spinner, copyInstead = fal
writeAccess = false;
}

if (await fs.exists(targetApplicationPath)) {
if (await fs.pathExists(targetApplicationPath)) {
spinner.stop();
const { confirm } = await inquirer.createPromptModule()({
type: 'confirm',
@@ -1,4 +1,4 @@
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';

export default async (dir) => {
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-promise';
import fs from 'fs-extra';
import path from 'path';
import readPackageJSON from './read-package-json';

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

if (packageJSON.devDependencies && packageJSON.devDependencies['electron-prebuilt-compile']) {
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import fs from 'fs-promise';
import fs from 'fs-extra';

import config from '../../src/util/config';

@@ -1,5 +1,5 @@
import { expect } from 'chai';
import fs from 'fs-promise';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';

@@ -16,31 +16,31 @@ describe('ensure-output', () => {
it('should delete the directory contents if it exists', async () => {
await fs.mkdirs(path.resolve(tmpPath, 'foo'));
fs.writeFileSync(path.resolve(tmpPath, 'foo', 'touchedFile'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(true);
expect(await fs.pathExists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(true);
await ensureDirectory(path.resolve(tmpPath, 'foo'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(false);
expect(await fs.pathExists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(false);
});

it('should create the directory if it does not exist', async () => {
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(false);
expect(await fs.pathExists(path.resolve(tmpPath, 'bar'))).to.equal(false);
await ensureDirectory(path.resolve(tmpPath, 'bar'));
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(true);
expect(await fs.pathExists(path.resolve(tmpPath, 'bar'))).to.equal(true);
});
});

describe('ensureFile', () => {
it('should delete the file if it exists', async () => {
await fs.mkdirs(path.resolve(tmpPath, 'foo'));
fs.writeFileSync(path.resolve(tmpPath, 'foo', 'touchedFile'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(true);
expect(await fs.pathExists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(true);
await ensureFile(path.resolve(tmpPath, 'foo'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(false);
expect(await fs.pathExists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(false);
});

it('should create the containing directory if it does not exist', async () => {
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(false);
expect(await fs.pathExists(path.resolve(tmpPath, 'bar'))).to.equal(false);
await ensureFile(path.resolve(tmpPath, 'bar', 'file'));
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(true);
expect(await fs.pathExists(path.resolve(tmpPath, 'bar'))).to.equal(true);
});
});

0 comments on commit 012b152

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