inkplugin
Version:
INK cli tools for plugin developers.
171 lines (147 loc) • 4.4 kB
JavaScript
const { Command, flags } = require("@oclif/command");
const fs = require("fs");
const path = require("path");
const pjson = require("../../package.json");
const defaultReadMeContent = `
This project was bootstrapped with [inkplugin](https://www.npmjs.com/package/inkplugin) cli tool.
For more information you can refer to INK Plugin dev [documentation](https://github.com/inkcontent/dev-manual).
`;
const mainJSContent = (moduleWorker) => `${moduleWorker
? "import INKAPI from '../inkapi.js'"
: "importScripts('../inkapi.js')"
}
INKAPI.ready(() => {
const UI= INKAPI.ui;
const IO= INKAPI.io;
const Editor= INKAPI.editor;
//your code here
})
`;
const defaultPkgJSON = (name, moduleWorker = false) => `{
"name": "${name}",
"version": "0.0.1",
"description": "",
"homepage": "",
"main": "src/main.js",
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1",
"build": "inkplugin package -o dist",
"hash": "inkplugin hash ./"
},
"author": "",
"license": "ISC",
"ink-plugin": {
"id": "${createPackageID()}",
"title": "${capitalize(name)}",
"icon": "sample.png",
"readme": "readme.md",
"module": ${moduleWorker}
},
"engines": {
"ink": ">2.0.0"
},
"devDependencies": {
"inkplugin": "^${pjson.version}"
}
}
`;
function capitalize(str = "") {
str = str.split("-");
for (var i = 0, x = str.length; i < x; i++) {
str[i] = str[i][0].toUpperCase() + str[i].substr(1);
}
return str.join(" ");
}
function createPackageID() {
var dt = new Date().getTime();
var pkId = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
var r = (dt + Math.random() * 16) % 16 | 0;
dt = Math.floor(dt / 16);
// eslint-disable-next-line no-mixed-operators
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
}
);
return pkId;
}
class CreateCommand extends Command {
async run() {
const { flags, args } = this.parse(CreateCommand);
const module = flags["module-worker"];
const location = args.path;
this.log(
"\n******************** Creating INK Plugin Project ********************\n"
);
this.log(`Generating: ${location}\n`);
this.log(
`${module ? "Module Worker Selected" : "Standard Worker Selected"}\n`
);
fs.mkdir(location, {}, (err) => {
if (err) throw err;
fs.appendFile(
path.join(location, "package.json"),
defaultPkgJSON(location, module),
function (err) {
if (err) throw err;
}
);
this.log("- package.json");
fs.appendFile(
path.join(location, "readme.md"),
defaultReadMeContent,
function (err) {
if (err) throw err;
}
);
this.log("- readme.md");
fs.mkdir(path.join(location, "src"), () => {
fs.appendFile(
path.join(location, "src", "main.js"),
mainJSContent(module),
function (err) {
if (err) throw err;
}
);
fs.mkdir(path.join(location, "src", "assets"), () => { });
});
this.log("- main.js");
fs.mkdir(path.join(location, "dist"), () => { });
fs.copyFile(
path.join(__dirname, `../INKAPI/inkapi${module ? ".module" : ""}.js`),
path.join(location, "inkapi.js"),
(err) => {
if (err) throw err;
}
);
this.log("- inkapi.js");
fs.copyFile(
path.join(__dirname, "../sample.png"),
path.join(location, "sample.png"),
(err) => {
if (err) throw err;
}
);
this.log("- sample.png\n");
});
}
}
CreateCommand.description = `creates starter INK plugin project.
With just single command you can generate starter plugin project that will have all the basic things ready to start writing your own INK plugin.
`;
CreateCommand.flags = {
"module-worker": flags.boolean({
char: "m",
description: "Enables ES6 Modules support for your plugin.",
}),
};
CreateCommand.args = [
{
name: "path", // name of arg to show in help and reference with args[name]
required: true, // make the arg required with `required: true`
description: "Path for project creation.", // help description
hidden: false, // hide this arg from help
},
];
module.exports = CreateCommand;