Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a14c4d
commit f83dbed
Showing
9 changed files
with
169 additions
and
25 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
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,19 @@ | ||
#!/usr/bin/env node | ||
const path = require("path"); | ||
const args = process.argv.slice(2); | ||
// This would be `require('plop')` in prod | ||
const { Plop, run } = require("../../../instrumented/src/plop.js"); | ||
const argv = require("minimist")(args); | ||
|
||
Plop.launch( | ||
{ | ||
cwd: argv.cwd, | ||
// In order for `plop` to always pick up the `plopfile.js` despite the CWD, you must use `__dirname` | ||
configPath: path.join(__dirname, "plopfile.js"), | ||
require: argv.require, | ||
completion: argv.completion, | ||
// This will merge the `plop` argv and the generator argv. | ||
// This means that you don't need to use `--` anymore | ||
}, | ||
(env) => run(env, undefined, true) | ||
); |
Empty file.
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,3 @@ | ||
{ | ||
"name": "plop-example-wrap" | ||
} |
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,37 @@ | ||
module.exports = function (plop) { | ||
plop.setGenerator("test", { | ||
description: "this is a test", | ||
prompts: [ | ||
{ | ||
type: "input", | ||
name: "name", | ||
message: "What is your name?", | ||
validate: function (value) { | ||
if (/.+/.test(value)) { | ||
return true; | ||
} | ||
return "name is required"; | ||
}, | ||
}, | ||
{ | ||
type: "checkbox", | ||
name: "toppings", | ||
message: "What pizza toppings do you like?", | ||
choices: [ | ||
{ name: "Cheese", value: "cheese", checked: true }, | ||
{ name: "Pepperoni", value: "pepperoni" }, | ||
{ name: "Pineapple", value: "pineapple" }, | ||
{ name: "Mushroom", value: "mushroom" }, | ||
{ name: "Bacon", value: "bacon", checked: true }, | ||
], | ||
}, | ||
], | ||
actions: [ | ||
{ | ||
type: "add", | ||
path: "./output/added.txt", | ||
templateFile: "./templates/to-add.txt", | ||
}, | ||
], | ||
}); | ||
}; |
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 @@ | ||
Hello |
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 |
---|---|---|
|
@@ -101,3 +101,5 @@ test("Should bypass prompt by name", async () => { | |
|
||
fireEvent.sigterm(); | ||
}); | ||
|
||
test.todo("Dynamic actions"); |
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,29 +1,35 @@ | ||
const { render, ...props } = require("cli-testing-library"); | ||
const { resolve } = require("path"); | ||
|
||
module.exports = { | ||
...props, | ||
/** | ||
* @param {Array} args | ||
* @param {Object} opts | ||
*/ | ||
renderPlop(args = [], opts = {}) { | ||
const { cwd = __dirname } = opts; | ||
/** | ||
* @param {String} script | ||
* @param {Array} args | ||
* @param {Object} opts | ||
*/ | ||
function renderScript(script, args = [], opts = {}) { | ||
const { cwd = __dirname } = opts; | ||
|
||
const rendered = render("npx", ["nyc", "--silent", "node", script, ...args], { | ||
cwd, | ||
}); | ||
|
||
const rendered = render( | ||
"npx", | ||
[ | ||
"nyc", | ||
"--silent", | ||
"node", | ||
resolve(__dirname, "../instrumented/bin/plop.js"), | ||
...args, | ||
], | ||
{ | ||
cwd, | ||
} | ||
); | ||
return rendered; | ||
} | ||
|
||
return rendered; | ||
}, | ||
/** | ||
* @param {Array} args | ||
* @param {Object} opts | ||
*/ | ||
function renderPlop(args = [], opts = {}) { | ||
return renderScript( | ||
resolve(__dirname, "../instrumented/bin/plop.js"), | ||
args, | ||
opts | ||
); | ||
} | ||
|
||
module.exports = { | ||
...props, | ||
renderScript, | ||
renderPlop, | ||
}; |
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,73 @@ | ||
const { renderScript, renderPlop } = require("./render"); | ||
const { resolve } = require("path"); | ||
const { waitFor } = require("cli-testing-library"); | ||
const fs = require("fs"); | ||
|
||
const renderWrapper = (...props) => { | ||
return renderScript( | ||
resolve(__dirname, "./examples/wrap-plop/index.js"), | ||
...props | ||
); | ||
}; | ||
|
||
test("wrapper should show version on v flag", async () => { | ||
const { findByText } = await renderWrapper(["-v"]); | ||
|
||
expect(await findByText(/^[\w\.-]+$/)).toBeTruthy(); | ||
}); | ||
|
||
test("wrapper should prompts", async () => { | ||
const { findByText, fireEvent } = await renderWrapper([""], { | ||
cwd: resolve(__dirname, "./examples/wrap-plop"), | ||
}); | ||
|
||
expect(await findByText("What is your name?")).toBeTruthy(); | ||
fireEvent.sigterm(); | ||
}); | ||
|
||
test("wrapper should bypass prompts with index", async () => { | ||
const { findByText, queryByText, fireEvent } = await renderWrapper( | ||
["Corbin"], | ||
{ | ||
cwd: resolve(__dirname, "./examples/wrap-plop"), | ||
} | ||
); | ||
|
||
expect(await queryByText("What is your name?")).toBeFalsy(); | ||
expect(await findByText("What pizza toppings do you like?")).toBeTruthy(); | ||
fireEvent.sigterm(); | ||
}); | ||
|
||
test("wrapper should bypass prompts with name", async () => { | ||
const { findByText, queryByText, fireEvent } = await renderWrapper( | ||
["--name", "Corbin"], | ||
{ | ||
cwd: resolve(__dirname, "./examples/wrap-plop"), | ||
} | ||
); | ||
|
||
expect(await queryByText("What is your name?")).toBeFalsy(); | ||
expect(await findByText("What pizza toppings do you like?")).toBeTruthy(); | ||
fireEvent.sigterm(); | ||
}); | ||
|
||
test("can run actions (add)", async () => { | ||
const { fireEvent } = await renderPlop(["Test", "Cheese"], { | ||
cwd: resolve(__dirname, "./examples/wrap-plop"), | ||
}); | ||
|
||
const expectedFilePath = resolve( | ||
__dirname, | ||
"./examples/wrap-plop/output/added.txt" | ||
); | ||
|
||
await waitFor(() => fs.promises.stat(expectedFilePath)); | ||
|
||
const data = fs.readFileSync(expectedFilePath, "utf8"); | ||
|
||
expect(data).toMatch(/Hello/); | ||
|
||
fs.unlinkSync(expectedFilePath); | ||
|
||
fireEvent.sigterm(); | ||
}); |