Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript support #427

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ Because [context switching is expensive](https://www.petrikainulainen.net/softwa
# Plopfile API
The plopfile api is the collection of methods that are exposed by the `plop` object. Most of the work is done by [`setGenerator`](#setgenerator) but this section documents the other methods that you may also find useful in your plopfile.

## TypeScript Declarations
## TypeScript Support

`plop` bundles TypeScript declarations. Whether or not you write your plopfile in TypeScript, many editors will offer code assistance via these declarations.
`plop` bundles TypeScript declarations and supports `plopfile.ts` using `plop --init-ts`. Whether or not you write your plopfile in TypeScript, many editors will offer code assistance via these declarations.

```javascript
// plopfile.ts
Expand Down
3 changes: 2 additions & 1 deletion packages/plop/bin/plop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
const args = process.argv.slice(2);
import { Plop, run } from "../src/plop.js";
import minimist from "minimist";
import { cwd } from 'process';
const argv = minimist(args);

Plop.prepare(
{
cwd: argv.cwd,
cwd: argv.cwd || cwd(),
preload: argv.preload || [],
configPath: argv.plopfile,
completion: argv.completion,
Expand Down
2 changes: 2 additions & 0 deletions packages/plop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"devDependencies": {
"cli-testing-library": "^2.0.2",
"glob": "^10.3.12",
"inquirer-directory": "^2.2.0",
"nyc": "^15.1.0",
"plop-pack-fancy-comments": "^0.2.1",
Expand All @@ -48,6 +49,7 @@
"minimist": "^1.2.8",
"node-plop": "^0.32.0",
"ora": "^8.0.0",
"tsx": "^4.7.2",
"v8flags": "^4.0.1"
},
"engines": {
Expand Down
45 changes: 28 additions & 17 deletions packages/plop/src/console-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function displayHelpScreen() {
" -t, --show-type-names " +
chalk.dim("Show type names instead of abbreviations"),
" -i, --init " + chalk.dim("Generate a basic plopfile.js"),
" --init-ts " + chalk.dim("Generate a basic plopfile.ts"),
" -v, --version " + chalk.dim("Print current version"),
" -f, --force " + chalk.dim("Run the generator forcefully"),
"",
Expand Down Expand Up @@ -89,25 +90,35 @@ function displayHelpScreen() {
);
}

function createInitPlopfile(force = false) {
var initString =
"export default function (plop) {\n\n" +
"\tplop.setGenerator('basics', {\n" +
"\t\tdescription: 'this is a skeleton plopfile',\n" +
"\t\tprompts: [],\n" +
"\t\tactions: []\n" +
"\t});\n\n" +
"};";
function createInitPlopfile(force = false, useTypescript = false) {
var initString = (()=>{
if(useTypescript) {
return "import type { NodePlopAPI } from 'plop'\n" +
"\n" +
"export default async function (plop: NodePlopAPI) {\n" +
"\n" +
"}\n" +
"\n";
} else {
return "export default function (plop) {\n\n" +
"\tplop.setGenerator('basics', {\n" +
"\t\tdescription: 'this is a skeleton plopfile',\n" +
"\t\tprompts: [],\n" +
"\t\tactions: []\n" +
"\t});\n\n" +
"};";
}
})();

if (fs.existsSync(process.cwd() + "/plopfile.js") && force === false) {
throw Error('"plopfile.js" already exists at this location.');
}

if (fs.existsSync(process.cwd() + "/plopfile.cjs") && force === false) {
throw Error('"plopfile.cjs" already exists at this location.');
}
[`js`, `cjs`, `ts`].forEach(ext => {
const name = `plopfile.${ext}`;
if (fs.existsSync(process.cwd() + `/${name}`) && force === false) {
throw Error(`"${name}" already exists at this location.`);
}
});

fs.writeFileSync(process.cwd() + "/plopfile.js", initString);
const outExt = useTypescript ? `ts` : `js`
fs.writeFileSync(process.cwd() + `/plopfile.${outExt}`, initString);
}

const typeDisplay = {
Expand Down
4 changes: 2 additions & 2 deletions packages/plop/src/input-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ function handleArgFlags(env) {
}

// handle request for initializing a new plopfile
if (argv.init || argv.i) {
if (argv.init || argv.i || argv[`init-ts`]) {
const force = argv.force === true || argv.f === true || false;
try {
out.createInitPlopfile(force);
out.createInitPlopfile(force, argv[`init-ts`]);
process.exit(0);
} catch (err) {
console.error(chalk.red("[PLOP] ") + err.message);
Expand Down
3 changes: 2 additions & 1 deletion packages/plop/src/plop.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import * as out from "./console-out.js";
import { combineBypassData } from "./bypass.js";
import { getBypassAndGenerator, handleArgFlags } from "./input-processing.js";

const extensions = { ...interpret.jsVariants, '.ts': 'tsx/dist/loader.cjs' }
const Plop = new Liftoff({
name: "plop",
extensions: interpret.jsVariants,
extensions,
v8flags: v8flags,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Options:
-h, --help Show this help display
-t, --show-type-names Show type names instead of abbreviations
-i, --init Generate a basic plopfile.js
--init-ts Generate a basic plopfile.ts
-v, --version Print current version
-f, --force Run the generator forcefully

Expand Down
2 changes: 1 addition & 1 deletion packages/plop/tests/examples/typescript/plopfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (plop: NodePlopAPI) {
{
type: "input",
name: "name",
message: "What is your name?",
message: "What is your Typescript name?",
validate: function (value) {
if (/.+/.test(value)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/plop/tests/examples/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"strict": true,
"baseUrl": "./",
"paths": {
"plop": ["../../src/plop.d.ts"]
"plop": ["../../../src/plop.d.ts"]
}
}
}
7 changes: 5 additions & 2 deletions packages/plop/tests/examples/wrap-plop/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node
import path from "node:path";
import { cwd } from 'process';
import { globSync } from "glob";
import minimist from "minimist";
import { Plop, run } from "../../../instrumented/src/plop.js";

Expand All @@ -11,10 +13,11 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));

Plop.prepare(
{
cwd: argv.cwd,
cwd: argv.cwd || cwd(),
preload: argv.preload || [],
// Use the plopfile in cwd if available, otherwise use the default plopfile
// In order for `plop` to always pick up the `plopfile.js` despite the CWD, you must use `__dirname`
configPath: path.join(__dirname, "plopfile.cjs"),
configPath: globSync(`plopfile.{cjs,js,ts}`, { cwd: argv.cwd, absolute: true })[0] || path.join(__dirname, "plopfile.cjs"),
completion: argv.completion,
// This will merge the `plop` argv and the generator argv.
// This means that you don't need to use `--` anymore
Expand Down
2 changes: 1 addition & 1 deletion packages/plop/tests/typescript.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ test("support typescript out of the box", async () => {
cwd: resolve(__dirname, "./examples/typescript"),
});

expect(await findByText("What is your name?")).toBeInTheConsole();
expect(await findByText("What is your Typescript name?")).toBeInTheConsole();
});
Loading