Skip to content

Commit

Permalink
feat(ncp): add ncp which uses ncp (#21)
Browse files Browse the repository at this point in the history
* feat(ncp): add `ncp` which uses `ncp`

Adds an alternative util to `copy (cpy-cli)` using `ncp` to allow recursive copying of directories.

closes #20

* fix(relativeizePaths): escape RegExp to match Windows paths

* fix(relativeizePaths): normalize Windows paths into Unix slashes

* fix(relativeizePaths): fix CI build errors

* fix(relativeizePaths): fix additional CI build errors

* fix(relativeizePaths): fix comma-dangle lint error
  • Loading branch information
Jerome Indefenzo authored and Kent C. Dodds committed Oct 3, 2017
1 parent 6753ad5 commit 4428f3e
Show file tree
Hide file tree
Showing 6 changed files with 472 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -75,6 +75,7 @@ API docs can be found [here](https://doc.esdoc.org/github.com/kentcdodds/nps-uti
- [ifWindows](https://doc.esdoc.org/github.com/kentcdodds/nps-utils/function/index.html#static-function-ifWindows)
- [ifNotWindows](https://doc.esdoc.org/github.com/kentcdodds/nps-utils/function/index.html#static-function-ifNotWindows)
- [copy](https://doc.esdoc.org/github.com/kentcdodds/nps-utils/function/index.html#static-function-copy)
- [ncp](https://doc.esdoc.org/github.com/kentcdodds/nps-utils/function/index.html#static-function-ncp)
- [mkdirp](https://doc.esdoc.org/github.com/kentcdodds/nps-utils/function/index.html#static-function-mkdirp)
- [open](https://doc.esdoc.org/github.com/kentcdodds/nps-utils/function/index.html#static-function-open)
- [crossEnv](https://doc.esdoc.org/github.com/kentcdodds/nps-utils/function/index.html#static-function-crossEnv)
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -23,6 +23,7 @@
"cross-env": "^3.1.4",
"is-windows": "^1.0.0",
"mkdirp": "^0.5.1",
"ncp": "2.0.0",
"opn-cli": "^3.1.0",
"rimraf": "^2.6.1"
},
Expand Down
8 changes: 6 additions & 2 deletions src/__snapshots__/index.test.js.snap
Expand Up @@ -32,6 +32,10 @@ exports[`mkdirp as darwin 1`] = `"node node_modules/mkdirp/bin/cmd.js /tmp/foo/b

exports[`mkdirp as win32 1`] = `"node node_modules/mkdirp/bin/cmd.js /tmp/foo/bar/baz"`;

exports[`ncp as darwin 1`] = `"node node_modules/ncp/bin/ncp src dist"`;

exports[`ncp as win32 1`] = `"node node_modules/ncp/bin/ncp src dist"`;

exports[`open as darwin 1`] = `"node node_modules/opn-cli/cli.js http://kentcdodds.com -- \\"google chrome\\" --incognito"`;

exports[`open as win32 1`] = `"node node_modules/opn-cli/cli.js http://kentcdodds.com -- \\"google chrome\\" --incognito"`;
Expand All @@ -44,9 +48,9 @@ exports[`runInNewWindow as darwin 1`] = `"osascript -e 'tell application \\"Term
exports[`runInNewWindow as win32 1`] = `"start cmd /k \\"cd <projectRootDir> && echo hi\\""`;
exports[`runInNewWindow.nps as darwin 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 darwin 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 win32 1`] = `"start cmd /k \\"cd <projectRootDir> && node node_modules/.bin/nps \\\\\\"lint --cache\\\\\\"\\""`;
exports[`runInNewWindow.nps as win32 1`] = `"start cmd /k \\"cd <projectRootDir> && node node_modules/.bin/nps /\\"lint --cache/\\"\\""`;
exports[`series as darwin 1`] = `"echo hey && echo hi && echo there"`;
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Expand Up @@ -21,6 +21,7 @@ export {
ifWindows,
ifNotWindows,
copy,
ncp,
mkdirp,
open,
crossEnv,
Expand Down Expand Up @@ -277,6 +278,18 @@ function copy(args) {
return `${getBin('cpy-cli', 'cpy')} ${args}`
}

/**
* Gets a script that uses the ncp binary. ncp
* is a dependency of nps-utils, so you don't need to
* install it yourself.
* @param {string} args - args to pass to ncp
* learn more from http://npm.im/ncp
* @return {string} - the command with the ncp binary
*/
function ncp(args) {
return `${getBin('ncp')} ${args}`
}

/**
* Gets a script that uses the mkdirp binary. mkdirp
* is a dependency of nps-utils, so you don't need to
Expand Down
13 changes: 11 additions & 2 deletions src/index.test.js
Expand Up @@ -44,6 +44,7 @@ const snapshotTests = {
ifWindows: ({ifWindows}) => ifWindows('echo main', 'echo alternate'),
ifNotWindows: ({ifNotWindows}) => ifNotWindows('echo main', 'echo alternate'),
copy: ({copy}) => copy('"**/*.html" "../dist/" --cwd=src --parents'),
ncp: ({ncp}) => ncp('src dist'),
mkdirp: ({mkdirp}) => mkdirp('/tmp/foo/bar/baz'),
open: ({open}) =>
open('http://kentcdodds.com -- "google chrome" --incognito'),
Expand Down Expand Up @@ -78,10 +79,18 @@ function withPlatform(platform, getResult) {
}

function relativeizePath(stringWithAbsolutePaths) {
return stringWithAbsolutePaths.replace(
new RegExp(path.resolve(__dirname, '../'), 'g'),
// escape string for regexp generation
const escapedPath = path.resolve(__dirname, '../').replace(
new RegExp('[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\^\\$\\|]', 'g'),
'\\$&',
)

const relativePath = stringWithAbsolutePaths.replace(
new RegExp(escapedPath, 'g'),
'<projectRootDir>',
)

return relativePath.replace(/\\/g, '/')
}

/*
Expand Down

0 comments on commit 4428f3e

Please sign in to comment.