forked from plopjs/plop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plopjs#297): added support for typescript files
- Loading branch information
Showing
12 changed files
with
450 additions
and
4 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
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 @@ | ||
import jiti from "jiti"; | ||
|
||
/** | ||
* Try to require a module, if it fails, return the errorReturn value | ||
* | ||
* @param {string} id - The module id to require | ||
* @param {string} rootDirectory - The root directory to require the module from | ||
* @param {any} errorReturn - The value to return if the require fails | ||
* | ||
* @returns {any} The required module or the errorReturn value | ||
*/ | ||
const tryRequire = (id, rootDirectory, errorReturn) => { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const _require = jiti(rootDirectory, { | ||
esmResolve: true, | ||
interopDefault: true, | ||
}); | ||
|
||
try { | ||
return _require(id); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (error) { | ||
if (error.code !== "MODULE_NOT_FOUND") { | ||
console.error( | ||
new Error(`Error trying import ${id} from ${rootDirectory}`, { | ||
cause: error, | ||
}), | ||
); | ||
} | ||
|
||
console.log(error) | ||
|
||
return errorReturn; | ||
} | ||
}; | ||
|
||
export default tryRequire; |
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
4 changes: 4 additions & 0 deletions
4
packages/node-plop/tests/typescript-plopfile/plop-templates/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,4 @@ | ||
add test | ||
name: {{name}} | ||
upperCase: {{constantCase name}} | ||
{{> salutation}} |
8 changes: 8 additions & 0 deletions
8
packages/node-plop/tests/typescript-plopfile/plop-templates/change-me.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,8 @@ | ||
the modify option in the test plop should add lines below for each run. | ||
Use modify for things like adding script references to an HTML file. | ||
|
||
-- APPEND ITEMS HERE -- | ||
|
||
+++++++++++++++++++++++++++++++++++++++ | ||
|
||
-- PREPEND ITEMS HERE -- |
2 changes: 2 additions & 0 deletions
2
packages/node-plop/tests/typescript-plopfile/plop-templates/part.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,2 @@ | ||
this is prepended! ## replace name here ##: {{age}} | ||
$1 |
115 changes: 115 additions & 0 deletions
115
packages/node-plop/tests/typescript-plopfile/plopfile.cts
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,115 @@ | ||
import fs from "fs"; | ||
import { join } from "path"; | ||
|
||
import { PlopCfg, NodePlopAPI } from "../../types"; | ||
|
||
export default function (plop: NodePlopAPI) { | ||
/////// | ||
// helpers are passed through to handlebars and made | ||
// available for use in the generator templates | ||
// | ||
|
||
// adds 4 dashes around some text (yes es6/es2015 is supported) | ||
plop.setHelper("dashAround", function (text) { | ||
return "---- " + text + " ----"; | ||
}); | ||
// plop.setHelper('dashAround', (text) => '---- ' + text + ' ----'); | ||
|
||
// formats an array of options like you would write | ||
// it if you were speaking (one, two, and three) | ||
plop.setHelper("wordJoin", function (words) { | ||
return words.join(", ").replace(/(:?.*),/, "$1, and"); | ||
}); | ||
|
||
// greet the user using a partial | ||
plop.setPartial( | ||
"salutation", | ||
"my name is {{ properCase name }} and I am {{ age }}.", | ||
); | ||
|
||
// setGenerator creates a generator that can be run with "plop generatorName" | ||
plop.setGenerator("basic-add", { | ||
description: "adds a file using a template", | ||
prompts: [ | ||
{ | ||
type: "input", | ||
name: "name", | ||
message: "What is your name?", | ||
validate: function (value) { | ||
if (/.+/.test(value)) { | ||
return true; | ||
} | ||
return "name is required"; | ||
}, | ||
}, | ||
{ | ||
type: "input", | ||
name: "age", | ||
message: "How old are you?", | ||
validate: function (value) { | ||
let digitsOnly = /\d+/; | ||
if (digitsOnly.test(value)) { | ||
return true; | ||
} | ||
return "Invalid age! Must be a number genius!"; | ||
}, | ||
}, | ||
], | ||
actions: [ | ||
{ | ||
type: "add", | ||
path: "src/{{dashCase name}}.txt", | ||
templateFile: "plop-templates/add.txt", | ||
abortOnFail: true, | ||
}, | ||
{ | ||
type: "add", | ||
path: "src/_{{constantCase name}}.txt", | ||
template: | ||
'test: {{pkg "name"}}\npropertyPathTest: {{pkg "config.nested[1]"}}\ninline template: {{name}}', | ||
abortOnFail: true, | ||
}, | ||
function customAction(answers) { | ||
// move the current working directory to the plop file path | ||
// this allows this action to work even when the generator is | ||
// executed from inside a subdirectory | ||
|
||
let plopFilePath = plop.getPlopfilePath(); | ||
|
||
// custom function can be synchronous or async (by returning a promise) | ||
let copiedMsg = "hey {{name}}, I copied change-me.txt for you", | ||
changeFile = "change-me.txt", | ||
toPath = join(plopFilePath, "src", changeFile), | ||
fromPath = join(plopFilePath, "plop-templates", changeFile); | ||
|
||
// you can use plop.renderString to render templates | ||
copiedMsg = plop.renderString(copiedMsg, answers); | ||
|
||
if (fs.existsSync(toPath)) { | ||
fs.unlinkSync(toPath); | ||
} | ||
|
||
fs.writeFileSync(toPath, fs.readFileSync(fromPath)); | ||
return copiedMsg; | ||
}, | ||
{ | ||
type: "modify", | ||
path: "src/change-me.txt", | ||
pattern: /(-- APPEND ITEMS HERE --)/gi, | ||
template: "$1\r\n{{name}}: {{age}}", | ||
}, | ||
{ | ||
type: "modify", | ||
path: "src/change-me.txt", | ||
pattern: /(-- PREPEND ITEMS HERE --)/gi, | ||
templateFile: "plop-templates/part.txt", | ||
}, | ||
{ | ||
type: "modify", | ||
path: "src/change-me.txt", | ||
pattern: /## replace name here ##/gi, | ||
template: "replaced => {{dashCase name}}", | ||
}, | ||
], | ||
}); | ||
} |
115 changes: 115 additions & 0 deletions
115
packages/node-plop/tests/typescript-plopfile/plopfile.mts
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,115 @@ | ||
import fs from "fs"; | ||
import { join } from "path"; | ||
|
||
import { PlopCfg, NodePlopAPI } from "../../types"; | ||
|
||
export default function (plop: NodePlopAPI, config: Partial<PlopCfg> = {}) { | ||
/////// | ||
// helpers are passed through to handlebars and made | ||
// available for use in the generator templates | ||
// | ||
|
||
// adds 4 dashes around some text (yes es6/es2015 is supported) | ||
plop.setHelper("dashAround", function (text) { | ||
return "---- " + text + " ----"; | ||
}); | ||
// plop.setHelper('dashAround', (text) => '---- ' + text + ' ----'); | ||
|
||
// formats an array of options like you would write | ||
// it if you were speaking (one, two, and three) | ||
plop.setHelper("wordJoin", function (words) { | ||
return words.join(", ").replace(/(:?.*),/, "$1, and"); | ||
}); | ||
|
||
// greet the user using a partial | ||
plop.setPartial( | ||
"salutation", | ||
"my name is {{ properCase name }} and I am {{ age }}.", | ||
); | ||
|
||
// setGenerator creates a generator that can be run with "plop generatorName" | ||
plop.setGenerator("basic-add", { | ||
description: "adds a file using a template", | ||
prompts: [ | ||
{ | ||
type: "input", | ||
name: "name", | ||
message: "What is your name?", | ||
validate: function (value) { | ||
if (/.+/.test(value)) { | ||
return true; | ||
} | ||
return "name is required"; | ||
}, | ||
}, | ||
{ | ||
type: "input", | ||
name: "age", | ||
message: "How old are you?", | ||
validate: function (value) { | ||
let digitsOnly = /\d+/; | ||
if (digitsOnly.test(value)) { | ||
return true; | ||
} | ||
return "Invalid age! Must be a number genius!"; | ||
}, | ||
}, | ||
], | ||
actions: [ | ||
{ | ||
type: "add", | ||
path: "src/{{dashCase name}}.txt", | ||
templateFile: "plop-templates/add.txt", | ||
abortOnFail: true, | ||
}, | ||
{ | ||
type: "add", | ||
path: "src/_{{constantCase name}}.txt", | ||
template: | ||
'test: {{pkg "name"}}\npropertyPathTest: {{pkg "config.nested[1]"}}\ninline template: {{name}}', | ||
abortOnFail: true, | ||
}, | ||
function customAction(answers) { | ||
// move the current working directory to the plop file path | ||
// this allows this action to work even when the generator is | ||
// executed from inside a subdirectory | ||
|
||
let plopFilePath = plop.getPlopfilePath(); | ||
|
||
// custom function can be synchronous or async (by returning a promise) | ||
let copiedMsg = "hey {{name}}, I copied change-me.txt for you", | ||
changeFile = "change-me.txt", | ||
toPath = join(plopFilePath, "src", changeFile), | ||
fromPath = join(plopFilePath, "plop-templates", changeFile); | ||
|
||
// you can use plop.renderString to render templates | ||
copiedMsg = plop.renderString(copiedMsg, answers); | ||
|
||
if (fs.existsSync(toPath)) { | ||
fs.unlinkSync(toPath); | ||
} | ||
|
||
fs.writeFileSync(toPath, fs.readFileSync(fromPath)); | ||
return copiedMsg; | ||
}, | ||
{ | ||
type: "modify", | ||
path: "src/change-me.txt", | ||
pattern: /(-- APPEND ITEMS HERE --)/gi, | ||
template: "$1\r\n{{name}}: {{age}}", | ||
}, | ||
{ | ||
type: "modify", | ||
path: "src/change-me.txt", | ||
pattern: /(-- PREPEND ITEMS HERE --)/gi, | ||
templateFile: "plop-templates/part.txt", | ||
}, | ||
{ | ||
type: "modify", | ||
path: "src/change-me.txt", | ||
pattern: /## replace name here ##/gi, | ||
template: "replaced => {{dashCase name}}", | ||
}, | ||
], | ||
}); | ||
} |
Oops, something went wrong.