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

Commit

Permalink
Merge pull request #1 from form8ion/alpha
Browse files Browse the repository at this point in the history
Initial version
  • Loading branch information
travi authored Apr 25, 2020
2 parents 1483ff7 + f113851 commit b3fc38e
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
extends:
- '@form8ion'
- '@form8ion/mocha'
- '@form8ion/mocha'

overrides:
- files: example.js
rules:
import/no-extraneous-dependencies: off
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ JavaScript language
* [Dependencies](#dependencies)
* [Verification](#verification)

## Features

* Adds scripts to `package.json` from results

### Coming Soon

* Install dependencies from results
* Configure eslint configs from results

## Usage

<!--consumer-badges start -->
Expand All @@ -36,8 +45,18 @@ $ npm install @form8ion/lift-javascript --save-prod

### Example

#### Import

```javascript
import {lift, test} from '@form8ion/lift-javascript';
```

#### Execute

```javascript
import liftJavascript from '@form8ion/lift-javascript';
if (test({projectRoot: process.cwd()})) {
lift({results: {dependencies: [], devDependencies: [], scripts: {}, elintConfigs: []}});
}
```

## Contributing
Expand Down
16 changes: 14 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// #### Import
// remark-usage-ignore-next
/* eslint-disable-next-line no-unused-vars */
import liftJavascript from './lib/index.cjs';
import stubbedFs from 'mock-fs';
import {lift, test} from './lib/index.cjs';

// remark-usage-ignore-next
stubbedFs();

// #### Execute

if (test({projectRoot: process.cwd()})) {
lift({results: {dependencies: [], devDependencies: [], scripts: {}, elintConfigs: []}});
}
// remark-usage-ignore-next
stubbedFs.restore();
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"husky": "4.2.5",
"lockfile-lint": "4.2.2",
"mocha": "7.1.1",
"mock-fs": "^4.12.0",
"npm-run-all": "4.1.5",
"nyc": "15.0.1",
"remark-cli": "8.0.0",
Expand Down
7 changes: 0 additions & 7 deletions src/canary-test.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as lift} from './lift';
export {default as test} from './test';
27 changes: 27 additions & 0 deletions src/lift-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +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 () => {
const projectRoot = any.string();
const scripts = any.simpleObject();
const results = {...any.simpleObject(), scripts};

await lift({projectRoot, results});

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

export default async function ({results, projectRoot}) {
await liftScripts({projectRoot, scripts: results.scripts});
}
44 changes: 44 additions & 0 deletions src/scripts-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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(JSON.stringify({...packageJsonContents, scripts: originalScripts}));

await liftScripts({projectRoot, scripts});

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

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

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

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

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

await fs.writeFile(
pathToPackageJson,
JSON.stringify(
{...existingPackageJsonContents, scripts: {...existingPackageJsonContents.scripts, ...scripts}},
null,
2
)
);
}
}
53 changes: 53 additions & 0 deletions src/test-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {promises as fs} from 'fs';
import sinon from 'sinon';
import {assert} from 'chai';
import any from '@travi/any';
import testApplicability from './test';

suite('applicability test', () => {
let sandbox;

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

sandbox.stub(fs, 'stat');
});

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

test('that `true` is returned if the project has an `.nvmrc` file', async () => {
const projectRoot = any.string();
fs.stat.withArgs(`${projectRoot}/.nvmrc`).returns({isFile: () => true});

assert.isTrue(await testApplicability({projectRoot}));
});

test('that `false` is returned if the project\'s `.nvmrc` is not file', async () => {
const projectRoot = any.string();
fs.stat.withArgs(`${projectRoot}/.nvmrc`).returns({isFile: () => false});

assert.isFalse(await testApplicability({projectRoot}));
});

test('that `false` is returned if the project does not have an `.nvmrc` file', async () => {
const error = new Error();
error.code = 'ENOENT';
fs.stat.rejects(error);

assert.isFalse(await testApplicability({}));
});

test('that errors other than for missing file', async () => {
const error = new Error();
error.code = any.word();
fs.stat.rejects(error);

try {
await testApplicability({});

throw new Error('an error should have been thrown by the module under test');
} catch (e) {
assert.equal(e, error);
}
});
});
13 changes: 13 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {promises as fs} from 'fs';

export default async function ({projectRoot}) {
try {
const stats = await fs.stat(`${projectRoot}/.nvmrc`);

return stats.isFile();
} catch (e) {
if ('ENOENT' === e.code) return false;

throw e;
}
}

0 comments on commit b3fc38e

Please sign in to comment.