Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
feat(scripts): updated scripts in the package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Apr 25, 2020
1 parent dad1e49 commit 0e62c55
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/lift-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import sinon from 'sinon';
import any from '@travi/any';
import {assert} from 'chai';
import * as scriptsLifter from './scripts';
import lift from './lift';

suite('lift', () => {
let sandbox;

setup(() => {
sandbox = sinon.createSandbox();

sandbox.stub(scriptsLifter, 'default');
});

teardown(() => sandbox.restore());

test('that results specific to js projects are lifted', async () => {
await lift();
const projectRoot = any.string();
const scripts = any.simpleObject();
const results = {...any.simpleObject(), scripts};

await lift({projectRoot, results});

assert.calledWith(scriptsLifter.default, {projectRoot, scripts});
});
});
4 changes: 3 additions & 1 deletion src/lift.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default function () {
import liftScripts from './scripts';

export default async function ({results, projectRoot}) {
await liftScripts({projectRoot, scripts: results.scripts});
}
42 changes: 42 additions & 0 deletions src/scripts-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {promises as fs} from 'fs';
import sinon from 'sinon';
import {assert} from 'chai';
import any from '@travi/any';
import liftScripts from './scripts';

suite('scripts lifter', () => {
let sandbox;
const projectRoot = any.string();

setup(() => {
sandbox = sinon.createSandbox();

sandbox.stub(fs, 'readFile');
sandbox.stub(fs, 'writeFile');
});

teardown(() => sandbox.restore());

test('that the scripts are added to the `package.json`', async () => {
const pathToPackageJson = `${projectRoot}/package.json`;
const scripts = any.simpleObject();
const originalScripts = any.simpleObject();
const packageJsonContents = any.simpleObject();
fs.readFile.withArgs(pathToPackageJson, 'utf8').resolves({...packageJsonContents, scripts: originalScripts});

await liftScripts({projectRoot, scripts});

assert.calledWith(
fs.writeFile,
pathToPackageJson,
{...packageJsonContents, scripts: {...originalScripts, ...scripts}}
);
});

test('that no updates are applied if no new scripts are provided', async () => {
await liftScripts({});

assert.notCalled(fs.readFile);
assert.notCalled(fs.writeFile);
});
});
14 changes: 14 additions & 0 deletions src/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {promises as fs} from 'fs';

export default async function ({projectRoot, scripts}) {
if (scripts) {
const pathToPackageJson = `${projectRoot}/package.json`;

const existingPackageJsonContents = await fs.readFile(pathToPackageJson, 'utf8');

await fs.writeFile(
pathToPackageJson,
{...existingPackageJsonContents, scripts: {...existingPackageJsonContents.scripts, ...scripts}}
);
}
}

0 comments on commit 0e62c55

Please sign in to comment.