Skip to content

Commit

Permalink
support for optional init file
Browse files Browse the repository at this point in the history
  • Loading branch information
skymen committed Dec 25, 2023
1 parent faa9ccf commit ba6b9ab
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 62 deletions.
199 changes: 138 additions & 61 deletions commands/scaffold.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,161 @@
const chalk = require('chalk');
const degit = require('degit');
const fs = require('fs');
const path = require('path');
const chalk = require("chalk");
const degit = require("degit");
const fs = require("fs");
const path = require("path");

const addonTemplate = {
plugin: {
url: 'https://github.com/ConstructFund/c3ide2-framework/plugins',
config: 'pluginConfig.js'
},
behavior: {
url: 'https://github.com/ConstructFund/c3ide2-framework/behaviors',
config: 'behaviorConfig.js'
},
effect: {
url: 'https://github.com/ConstructFund/c3ide2-framework/effects',
config: 'effectConfig.js'
},
theme: {
url: 'https://github.com/ConstructFund/c3ide2-framework/themes',
config: 'themeConfig.js'
}
}
plugin: {
url: "https://github.com/ConstructFund/c3ide2-framework/plugins",
config: "pluginConfig.js",
},
behavior: {
url: "https://github.com/ConstructFund/c3ide2-framework/behaviors",
config: "behaviorConfig.js",
},
effect: {
url: "https://github.com/ConstructFund/c3ide2-framework/effects",
config: "effectConfig.js",
},
theme: {
url: "https://github.com/ConstructFund/c3ide2-framework/themes",
config: "themeConfig.js",
},
};

async function create(type, params) {
validateValues(type, params);
const path = await cloneTemplate(type, params);
updateAddonPlaceholder(path, type, params);
validateValues(type, params);
const path = await cloneTemplate(type, params);
await installDependencies(path);
await maybeExecuteInitScript(path);
updateAddonPlaceholder(path, type, params);
}

function validateValues(type, params) {
if (!Object.keys(addonTemplate).includes(type)) {
throw new Error(`${chalk.red.bold("invalid addon type, please select one of the following: plugin, behavior, effect, theme")}`);
}
if (!Object.keys(addonTemplate).includes(type)) {
throw new Error(
`${chalk.red.bold(
"invalid addon type, please select one of the following: plugin, behavior, effect, theme"
)}`
);
}

if (params.addonName === undefined || params.addonName === null || params.addonName === '') {
throw new Error(`${chalk.red.bold("please provide addon name")}`);
}
if (
params.addonName === undefined ||
params.addonName === null ||
params.addonName === ""
) {
throw new Error(`${chalk.red.bold("please provide addon name")}`);
}

if (params.addonAuthor === undefined || params.addonAuthor === null || params.addonAuthor === '') {
throw new Error(`${chalk.red.bold("please provide addon author")}`);
}
if (
params.addonAuthor === undefined ||
params.addonAuthor === null ||
params.addonAuthor === ""
) {
throw new Error(`${chalk.red.bold("please provide addon author")}`);
}
}

function generateAddonID(params) {
const {addonName, addonAuthor} = params;
const { addonName, addonAuthor } = params;

const addonId = `${addonAuthor}_${addonName}`.toLowerCase().replace(/ /g, '_');
if (fs.existsSync(addonId)) {
throw new Error(`${chalk.red.bold("a folder with plugin ID : "+addonId+" already exists")}`);
}
const addonId = `${addonAuthor}_${addonName}`
.toLowerCase()
.replace(/ /g, "_");
if (fs.existsSync(addonId)) {
throw new Error(
`${chalk.red.bold(
"a folder with plugin ID : " + addonId + " already exists"
)}`
);
}

fs.mkdirSync(addonId);
console.log(`${chalk.green.bold("addon folder "+addonId+" created successfully")}`);
return addonId;
fs.mkdirSync(addonId);
console.log(
`${chalk.green.bold("addon folder " + addonId + " created successfully")}`
);
return addonId;
}

async function cloneTemplate(type, params) {
console.log(`${chalk.white.bold("cloning plugin template ...")}`);
const addonId = generateAddonID(params);
const emitter = degit(addonTemplate[type].url, { cache: false, force: true, verbose: true });

const addonPath = path.resolve(process.cwd(), addonId)
await emitter.clone(addonPath);

console.log(`${chalk.green.bold("plugin template cloned successfully")}`);
return addonPath;
console.log(`${chalk.white.bold("cloning plugin template ...")}`);
const addonId = generateAddonID(params);
const emitter = degit(addonTemplate[type].url, {
cache: false,
force: true,
verbose: true,
});

const addonPath = path.resolve(process.cwd(), addonId);
await emitter.clone(addonPath);

console.log(`${chalk.green.bold("plugin template cloned successfully")}`);
return addonPath;
}

function updateAddonPlaceholder(addonPath, type, params) {
const {addonName, addonAuthor} = params;
const templateFile = addonTemplate[type].config;
const addonConfigPath = path.resolve(process.cwd(), addonPath, 'src', templateFile);
const addonId = `${addonAuthor}_${addonName}`.toLowerCase().replace(/ /g, '_');

let config = fs.readFileSync(addonConfigPath, 'utf8');
config = config.replace(/<@ADDON_ID>/g, addonId);
config = config.replace(/<@ADDON_NAME>/g, addonName);
config = config.replace(/<@ADDON_AUTHOR>/g, addonAuthor);

fs.writeFileSync(addonConfigPath, config);
const { addonName, addonAuthor } = params;
const templateFile = addonTemplate[type].config;
const addonConfigPath = path.resolve(
process.cwd(),
addonPath,
"src",
templateFile
);
const addonId = `${addonAuthor}_${addonName}`
.toLowerCase()
.replace(/ /g, "_");

let config = fs.readFileSync(addonConfigPath, "utf8");
config = config.replace(/<@ADDON_ID>/g, addonId);
config = config.replace(/<@ADDON_NAME>/g, addonName);
config = config.replace(/<@ADDON_AUTHOR>/g, addonAuthor);

fs.writeFileSync(addonConfigPath, config);
}

function installDependencies(addonPath) {
return new Promise((resolve, reject) => {
console.log(`${chalk.white.bold("installing dependencies ...")}`);
const addonConfigPath = path.resolve(process.cwd(), addonPath);
const { exec } = require("child_process");
exec("npm install", { cwd: addonConfigPath }, (err, stdout, stderr) => {
if (err) {
console.error(err);
reject(err);
return;
}
console.log(`${chalk.green.bold("dependencies installed successfully")}`);
resolve();
});
});
}

function maybeExecuteInitScript(addonPath) {
return new Promise((resolve, reject) => {
const initScriptPath = path.resolve(process.cwd(), addonPath, "init.js");
if (fs.existsSync(initScriptPath)) {
console.log(`${chalk.white.bold("executing init script ...")}`);
const { exec } = require("child_process");
exec(
"node init.js",
{ cwd: path.resolve(process.cwd(), addonPath) },
(err, stdout, stderr) => {
if (err) {
console.error(err);
reject(err);
return;
}
console.log(
`${chalk.green.bold("init script executed successfully")}`
);
resolve();
}
);
} else {
resolve();
}
});
}

module.exports = create;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "c3ide2-cli",
"description": "A simple CLI for scaffolding construct 3 addons using c3ide2-framework",
"version": "1.1.0",
"version": "1.2.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
Expand Down

0 comments on commit ba6b9ab

Please sign in to comment.