Skip to content

Commit

Permalink
feat(rimraf): add rimraf script
Browse files Browse the repository at this point in the history
Also refactor tests slightly

Closes #3
  • Loading branch information
Kent C. Dodds committed Mar 2, 2017
1 parent b61317f commit 1f4648b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 64 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"any-shell-escape": "^0.1.1",
"common-tags": "^1.4.0",
"concurrently": "^3.4.0",
"is-windows": "^1.0.0"
"is-windows": "^1.0.0",
"rimraf": "^2.6.1"
},
"devDependencies": {
"all-contributors-cli": "^4.0.1",
Expand All @@ -39,7 +40,6 @@
"nps": "^5.0.3",
"opt-cli": "^1.5.1",
"prettier-eslint": "^4.2.0",
"rimraf": "^2.6.1",
"semantic-release": "^6.3.6",
"validate-commit-msg": "^2.8.2"
},
Expand Down
4 changes: 4 additions & 0 deletions src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ exports[`concurrent 1`] = `"node node_modules/concurrently/src/main.js --kill-ot

exports[`concurrent.nps 1`] = `"node node_modules/concurrently/src/main.js --kill-others-on-fail --prefix-colors \\"bgMagenta.bold,bgBlack.bold,bgCyan.bold,bgGreen.dim\\" --prefix \\"[{name}]\\" --names \\"test,lint,build.app,validate\\" 'nps test' 'nps lint' 'nps \\"build.app --silent\\"' 'nps validate'"`;

exports[`rimraf 1`] = `"node node_modules/rimraf/bin.js build"`;

exports[`runInNewWindow 1`] = `"osascript -e 'tell application \\"Terminal\\"' -e 'tell application \\"System Events\\" to keystroke \\"t\\" using {command down}' -e 'do script \\"cd <projectRootDir> && echo hi\\" in front window' -e 'end tell'"`;
exports[`runInNewWindow.nps 1`] = `"osascript -e 'tell application \\"Terminal\\"' -e 'tell application \\"System Events\\" to keystroke \\"t\\" using {command down}' -e 'do script \\"cd <projectRootDir> && node node_modules/.bin/nps \\\\\\"lint --cache\\\\\\"\\" in front window' -e 'end tell'"`;
exports[`runInNewWindow.nps as windows 1`] = `"start cmd /k \\"cd <projectRootDir> && node node_modules/.bin/nps \\\\\\"initiate database\\\\\\"\\""`;
exports[`series 1`] = `"echo hey && echo hi && echo there"`;
Expand Down
35 changes: 24 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const defaultColors = [
// TODO: add more colors that look good?
]

export {concurrent, series, runInNewWindow}
export {concurrent, series, runInNewWindow, rimraf}

/**
* Accepts any number of scripts, filters out any
Expand Down Expand Up @@ -102,7 +102,7 @@ function concurrent(scripts) {
`--names "${names.join(',')}"`,
shellEscape(quotedScripts),
]
const concurrently = getConcurrentlyBin()
const concurrently = getBin('concurrently')
return `${concurrently} ${flags.join(' ')}`

function reduceScripts(accumulator, scriptName, index) {
Expand Down Expand Up @@ -211,21 +211,34 @@ runInNewWindow.nps = function runInNewWindowNPS(scriptName) {
)
}

/**
* Gets a script that uses the rimraf binary
* @param {string} args - args to pass to rimraf
* learn more from http://npm.im/rimraf
* @return {string} - the command with the rimraf binary
*/
function rimraf(args) {
return `${getBin('rimraf')} ${args}`
}

// utils

function quoteScript(script, escaped) {
const quote = escaped ? '\\"' : '"'
const shouldQuote = script.indexOf(' ') !== -1
return shouldQuote ? `${quote}${script}${quote}` : script
}

function getConcurrentlyBin() {
const concurrentlyPackagePath = require.resolve('concurrently/package.json')
const concurrentlyDir = path.dirname(concurrentlyPackagePath)
const {bin: {concurrently: relativeConcurrentlyBin}} = require(
concurrentlyPackagePath,
)
const fullConcurrently = path.join(concurrentlyDir, relativeConcurrentlyBin)
const relativeConcurrently = path.relative(process.cwd(), fullConcurrently)
return `node ${relativeConcurrently}`
function getBin(packageName, binName = packageName) {
const packagePath = require.resolve(`${packageName}/package.json`)
const concurrentlyDir = path.dirname(packagePath)
let {bin: binRelativeToPackage} = require(packagePath)
if (typeof binRelativeToPackage === 'object') {
binRelativeToPackage = binRelativeToPackage[binName]
}
const fullBinPath = path.join(concurrentlyDir, binRelativeToPackage)
const relativeBinPath = path.relative(process.cwd(), fullBinPath)
return `node ${relativeBinPath}`
}

/*
Expand Down
91 changes: 40 additions & 51 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,46 @@
import path from 'path'
import {concurrent, series, runInNewWindow} from '.'
import {concurrent, series, runInNewWindow, rimraf} from '.'

test('series', () => {
expect(
series('echo hey', null, 'echo hi', undefined, 'echo there', false),
).toMatchSnapshot()
})

test('series.nps', () => {
expect(
series.nps(
'test',
false,
'lint.src',
null,
'lint.scripts --cache',
undefined,
' ',
'build --fast',
),
).toMatchSnapshot()
})

test('concurrent', () => {
expect(
concurrent({
test: 'echo test',
validate: {
script: false,
},
lint: {script: 'echo lint', color: 'bgWhite.black.dim'},
build: false,
cover: undefined,
}),
).toMatchSnapshot()
})

test('concurrent.nps', () => {
expect(
concurrent.nps(
null,
'test',
undefined,
'lint',
{script: 'build.app --silent'},
false,
{script: 'validate', color: 'bgGreen.dim'},
),
).toMatchSnapshot()
})
const snapshotTests = {
series: series('echo hey', null, 'echo hi', undefined, 'echo there', false),
'series.nps': series.nps(
'test',
false,
'lint.src',
null,
'lint.scripts --cache',
undefined,
' ',
'build --fast',
),
concurrent: concurrent({
test: 'echo test',
validate: {
script: false,
},
lint: {script: 'echo lint', color: 'bgWhite.black.dim'},
build: false,
cover: undefined,
}),
'concurrent.nps': concurrent.nps(
null,
'test',
undefined,
'lint',
{script: 'build.app --silent'},
false,
{script: 'validate', color: 'bgGreen.dim'},
),
runInNewWindow: runInNewWindow('echo hi'),
'runInNewWindow.nps': runInNewWindow.nps('lint --cache'),
rimraf: rimraf('build'),
}

test('runInNewWindow', () => {
expect(relativeizePath(runInNewWindow('echo hi'))).toMatchSnapshot()
Object.keys(snapshotTests).forEach(testName => {
test(testName, () => {
const result = snapshotTests[testName]
expect(relativeizePath(result)).toMatchSnapshot()
})
})

test('runInNewWindow.nps as windows', () => {
Expand Down

0 comments on commit 1f4648b

Please # to comment.