This repository has been archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scripts): updated scripts in the package.json
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
); | ||
} | ||
} |