Skip to content

Commit

Permalink
feat: Added --profile-create-new option
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilpanchal committed Nov 3, 2020
1 parent 55931e7 commit 849835d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/cmd/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* @flow */
import { fs } from 'mz';

import defaultBuildExtension from './build';
import {
showDesktopNotification as defaultDesktopNotifications,
Expand All @@ -8,6 +10,7 @@ import {
connectWithMaxRetries as defaultFirefoxClient,
} from '../firefox/remote';
import {createLogger} from '../util/logger';
import {WebExtError} from '../errors';
import defaultGetValidatedManifest from '../util/manifest';
import {
createExtensionRunner,
Expand All @@ -28,6 +31,7 @@ export type CmdRunParams = {|
pref?: FirefoxPreferences,
firefox: string,
firefoxProfile?: string,
profileCreateNew?: boolean,
ignoreFiles?: Array<string>,
keepProfileChanges: boolean,
noInput?: boolean,
Expand Down Expand Up @@ -72,6 +76,7 @@ export default async function run(
pref,
firefox,
firefoxProfile,
profileCreateNew,
keepProfileChanges = false,
ignoreFiles,
noInput = false,
Expand Down Expand Up @@ -117,6 +122,18 @@ export default async function run(
const customPrefs = pref;
const manifestData = await getValidatedManifest(sourceDir);

const profileDir = firefoxProfile || chromiumProfile;

if (profileCreateNew && profileDir) {
const isDir = fs.existsSync(profileDir);
if (isDir) {
throw new WebExtError(`Directory ${profileDir} already exists.`);
} else {
log.info(`Profile directory not found. Creating directory ${profileDir}`);
fs.mkdirSync(profileDir);
}
}

const runners = [];

const commonRunnerParams = {
Expand Down
7 changes: 7 additions & 0 deletions src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,13 @@ Example: $0 --help run.
demandOption: false,
type: 'string',
},
'profile-create-new': {
describe: 'Create a new profile in the directory specified by the ' +
'-p option if it doesn\'t already exist. If it already ' +
'exists, an error will be shown.',
demandOption: false,
type: 'boolean',
},
'keep-profile-changes': {
describe: 'Run Firefox directly in custom profile. Any changes to ' +
'the profile will be saved.',
Expand Down
47 changes: 45 additions & 2 deletions tests/unit/test-cmd/test.run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */
import path from 'path';

import { fs } from 'mz';
import {afterEach, beforeEach, describe, it} from 'mocha';
import {assert} from 'chai';
import sinon from 'sinon';
Expand Down Expand Up @@ -325,5 +326,47 @@ describe('run', () => {
sourceDir: '/fake/source/dir',
},
);
});
});
}
);

it('creates dir when profile doesn\'t exist using profile-create-new',
async () => {
sinon.stub(fs, 'existsSync').returns(false);
sinon.stub(fs, 'mkdirSync');

const firefoxProfile = '/pretend/path/to/Firefox/profile';
const cmd = prepareRun();

await cmd.run({
firefoxProfile,
profileCreateNew: true,
});

sinon.assert.calledWith(fs.mkdirSync, firefoxProfile);

fs.existsSync.restore();
fs.mkdirSync.restore();
}
);

it('throws error when profile dir already exists using profile-create-new',
async () => {
sinon.stub(fs, 'existsSync').returns(true);

const firefoxProfile = '/pretend/path/to/Firefox/profile';
const cmd = prepareRun();

const promise = cmd.run({
firefoxProfile,
profileCreateNew: true,
});

await assert.isRejected(
promise,
`Directory ${firefoxProfile} already exists.`
);

fs.existsSync.restore();
}
);
});

0 comments on commit 849835d

Please sign in to comment.