Skip to content

Commit

Permalink
Initial work to migrate to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 24, 2021
1 parent d152d6d commit 656a74f
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 206 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js → .eslintrc.cjs
Expand Up @@ -4,6 +4,10 @@ module.exports = {
es2021: true,
node: true,
},
parserOptions: {
sourceType: "module",
"allowImportExportEverywhere": true
},
extends: ["plugin:prettier/recommended"],
rules: {
// https://github.com/plopjs/plop/issues/288
Expand Down
5 changes: 3 additions & 2 deletions bin/plop.js
@@ -1,7 +1,8 @@
#!/usr/bin/env node
const args = process.argv.slice(2);
const { Plop, run } = require("../src/plop");
const argv = require("minimist")(args);
import { Plop, run } from "../src/plop.js";
import minimist from "minimist";
const argv = minimist(args);

Plop.launch(
{
Expand Down
7 changes: 5 additions & 2 deletions jest.config.js
@@ -1,4 +1,6 @@
module.exports = {
import { resolve } from "node:path";

export default {
coverageThreshold: {
global: {
branches: 40,
Expand All @@ -7,5 +9,6 @@ module.exports = {
statements: 50,
},
},
projects: [require.resolve("./tests/config/jest.config.js")],
projects: [resolve("./tests/config/jest.config.js")],
transform: {},
};
10 changes: 5 additions & 5 deletions package.json
@@ -1,8 +1,9 @@
{
"name": "plop",
"version": "2.7.6",
"version": "3.0.0-alpha.1",
"description": "Micro-generator framework that makes it easy for an entire team to create files with a level of uniformity",
"main": "./src/plop",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/plopjs/plop.git"
Expand All @@ -23,7 +24,7 @@
"url": "https://github.com/plopjs/plop/issues"
},
"scripts": {
"test": "npm run test:instrument && jest && nyc report",
"test": "npm run test:instrument && node --experimental-vm-modules node_modules/jest/bin/jest.js && nyc report",
"test:instrument": "nyc instrument ./bin ./instrumented/bin && nyc instrument ./src ./instrumented/src && cp package.json ./instrumented",
"format": "eslint --fix ./",
"prepare": "husky install"
Expand All @@ -33,7 +34,6 @@
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"execa": "^5.0.0",
"husky": "^7.0.2",
"inquirer-directory": "^2.2.0",
"jest": "^27.3.1",
Expand All @@ -56,9 +56,9 @@
"v8flags": "^2.0.10"
},
"engines": {
"node": ">=8.9.4"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"preferGlobal": "true",
"preferGlobal": true,
"bin": {
"plop": "./bin/plop.js"
},
Expand Down
8 changes: 3 additions & 5 deletions src/bypass.js
@@ -1,9 +1,7 @@
const chalk = require("chalk");
const out = require("./console-out");
import chalk from "chalk";
import * as out from "./console-out.js";

module.exports = {
combineBypassData,
};
export { combineBypassData };

/**
* Combine different types of bypass data
Expand Down
237 changes: 116 additions & 121 deletions src/console-out.js
@@ -1,132 +1,127 @@
"use strict";

const chalk = require("chalk");
const nodePlop = require("node-plop");
const fs = require("fs");
import chalk from "chalk";
import nodePlop from "node-plop";
import fs from "node:fs";

const defaultChoosingMessage =
chalk.blue("[PLOP]") + " Please choose a generator.";

module.exports = (function () {
function getHelpMessage(generator) {
const maxLen = Math.max(
...generator.prompts.map((prompt) => prompt.name.length)
);
console.log(
[
"",
chalk.bold("Options:"),
...generator.prompts.map(
(prompt) =>
" --" +
prompt.name +
" ".repeat(maxLen - prompt.name.length + 2) +
chalk.dim(prompt.help ? prompt.help : prompt.message)
),
].join("\n")
);
}

function chooseOptionFromList(plopList, message) {
const plop = nodePlop();
const generator = plop.setGenerator("choose", {
prompts: [
{
type: "list",
name: "generator",
message: message || defaultChoosingMessage,
choices: plopList.map(function (p) {
return {
name:
p.name +
chalk.gray(!!p.description ? " - " + p.description : ""),
value: p.name,
};
}),
},
],
});
return generator.runPrompts().then((results) => results.generator);
}
function getHelpMessage(generator) {
const maxLen = Math.max(
...generator.prompts.map((prompt) => prompt.name.length)
);
console.log(
[
"",
chalk.bold("Options:"),
...generator.prompts.map(
(prompt) =>
" --" +
prompt.name +
" ".repeat(maxLen - prompt.name.length + 2) +
chalk.dim(prompt.help ? prompt.help : prompt.message)
),
].join("\n")
);
}

function displayHelpScreen() {
console.log(
[
"",
chalk.bold("Usage:"),
" $ plop " +
chalk.dim("Select from a list of available generators"),
" $ plop <name> " +
chalk.dim("Run a generator registered under that name"),
" $ plop <name> [input] " +
chalk.dim("Run the generator with input data to bypass prompts"),
"",
chalk.bold("Options:"),
" -h, --help " + chalk.dim("Show this help display"),
" -t, --show-type-names " +
chalk.dim("Show type names instead of abbreviations"),
" -i, --init " + chalk.dim("Generate a basic plopfile.js"),
" -v, --version " + chalk.dim("Print current version"),
" -f, --force " + chalk.dim("Run the generator forcefully"),
"",
chalk.dim(" ------------------------------------------------------"),
chalk.dim(" ⚠ danger waits for those who venture below the line"),
"",
chalk.dim(" --plopfile Path to the plopfile"),
chalk.dim(
" --cwd Directory from which relative paths are calculated against while locating the plopfile"
),
chalk.dim(
" --require String or array of modules to require before running plop"
),
chalk.dim(
" --dest Output to this directory instead of the plopfile's parent directory"
),
"",
chalk.bold("Examples:"),
" $ " + chalk.blue("plop"),
" $ " + chalk.blue("plop component"),
" $ " + chalk.blue('plop component "name of component"'),
"",
].join("\n")
);
}
function chooseOptionFromList(plopList, message) {
const plop = nodePlop();
const generator = plop.setGenerator("choose", {
prompts: [
{
type: "list",
name: "generator",
message: message || defaultChoosingMessage,
choices: plopList.map(function (p) {
return {
name:
p.name + chalk.gray(!!p.description ? " - " + p.description : ""),
value: p.name,
};
}),
},
],
});
return generator.runPrompts().then((results) => results.generator);
}

function createInitPlopfile(force = false) {
var initString =
"module.exports = 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 displayHelpScreen() {
console.log(
[
"",
chalk.bold("Usage:"),
" $ plop " +
chalk.dim("Select from a list of available generators"),
" $ plop <name> " +
chalk.dim("Run a generator registered under that name"),
" $ plop <name> [input] " +
chalk.dim("Run the generator with input data to bypass prompts"),
"",
chalk.bold("Options:"),
" -h, --help " + chalk.dim("Show this help display"),
" -t, --show-type-names " +
chalk.dim("Show type names instead of abbreviations"),
" -i, --init " + chalk.dim("Generate a basic plopfile.js"),
" -v, --version " + chalk.dim("Print current version"),
" -f, --force " + chalk.dim("Run the generator forcefully"),
"",
chalk.dim(" ------------------------------------------------------"),
chalk.dim(" ⚠ danger waits for those who venture below the line"),
"",
chalk.dim(" --plopfile Path to the plopfile"),
chalk.dim(
" --cwd Directory from which relative paths are calculated against while locating the plopfile"
),
chalk.dim(
" --require String or array of modules to require before running plop"
),
chalk.dim(
" --dest Output to this directory instead of the plopfile's parent directory"
),
"",
chalk.bold("Examples:"),
" $ " + chalk.blue("plop"),
" $ " + chalk.blue("plop component"),
" $ " + chalk.blue('plop component "name of component"'),
"",
].join("\n")
);
}

if (fs.existsSync(process.cwd() + "/plopfile.js") && force === false) {
throw Error('"plopfile.js" already exists at this location.');
}
function createInitPlopfile(force = false) {
var initString =
"module.exports = 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" +
"};";

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

const typeDisplay = {
function: chalk.yellow("->"),
add: chalk.green("++"),
addMany: chalk.green("+!"),
modify: `${chalk.green("+")}${chalk.red("-")}`,
append: chalk.green("_+"),
skip: chalk.green("--"),
};
const typeMap = (name, noMap) => {
const dimType = chalk.dim(name);
return noMap ? dimType : typeDisplay[name] || dimType;
};
fs.writeFileSync(process.cwd() + "/plopfile.js", initString);
}

const typeDisplay = {
function: chalk.yellow("->"),
add: chalk.green("++"),
addMany: chalk.green("+!"),
modify: `${chalk.green("+")}${chalk.red("-")}`,
append: chalk.green("_+"),
skip: chalk.green("--"),
};
const typeMap = (name, noMap) => {
const dimType = chalk.dim(name);
return noMap ? dimType : typeDisplay[name] || dimType;
};

return {
chooseOptionFromList,
displayHelpScreen,
createInitPlopfile,
typeMap,
getHelpMessage,
};
})();
export {
chooseOptionFromList,
displayHelpScreen,
createInitPlopfile,
typeMap,
getHelpMessage,
};
12 changes: 7 additions & 5 deletions src/input-processing.js
@@ -1,12 +1,12 @@
const chalk = require("chalk");
const minimist = require("minimist");
const out = require("./console-out");
import chalk from "chalk";
import minimist from "minimist";
import * as out from "./console-out.js";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const globalPkg = require("../package.json");
const args = process.argv.slice(2);
const argv = minimist(args);

module.exports = { getBypassAndGenerator, handleArgFlags };

/**
* Parses the user input to identify the generator to run and any bypass data
* @param plop - The plop context
Expand Down Expand Up @@ -101,3 +101,5 @@ function handleArgFlags(env) {
process.exit(1);
}
}

export { getBypassAndGenerator, handleArgFlags };
2 changes: 1 addition & 1 deletion src/plop.d.ts
@@ -1,4 +1,4 @@
import Liftoff = require("liftoff");
import * as Liftoff from "liftoff";

export {
ActionConfig,
Expand Down

0 comments on commit 656a74f

Please sign in to comment.