Skip to content

Instantly share code, notes, and snippets.

@letanure
Last active October 26, 2021 18:35
Show Gist options
  • Save letanure/ed0ea13e08fe1c076948ea7228c2fb78 to your computer and use it in GitHub Desktop.
Save letanure/ed0ea13e08fe1c076948ea7228c2fb78 to your computer and use it in GitHub Desktop.
NodeJs utility to copy files from Strapi admin to the current project and overwrite behaviors
// npm install inquirer-fuzzy-path inquirer ncp -D
// or
// yarn add inquirer-fuzzy-path inquirer ncp -D
const inquirer = require("inquirer");
const fs = require("fs");
const ncp = require("ncp").ncp;
inquirer.registerPrompt("fuzzypath", require("inquirer-fuzzy-path"));
async function getFilePath() {
return await inquirer.prompt([
{
type: "fuzzypath",
name: "file",
itemType: "any",
message: "Select a file OR folder to copy",
rootPath: "node_modules/strapi-admin/admin",
suggestOnly: false,
},
]);
}
function pathIsFileOrFolder(path) {
return fs.lstatSync(path).isFile() || fs.lstatSync(path).isDirectory();
}
async function createFolder(path) {
const dir = path.substring(0, path.lastIndexOf("/"));
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
async function copyFolder(source, target) {
await createFolder(target);
ncp(source, target, function (err) {
if (err) throw err;
console.log(`Folder ${source} was copied!`);
})
}
async function copyFile(source, target) {
await createFolder(target);
fs.copyFile(source, target, (err) => {
if (err) throw err;
console.log(`File ${source} was copied!`);
});
}
async function start() {
const answers = await getFilePath();
const pathNodeString = answers?.file;
const pathProjectString = pathNodeString.replace('node_modules/strapi-admin/', '')
if (fs.lstatSync(pathNodeString).isDirectory()) {
copyFolder(pathNodeString, pathProjectString);
} else {
copyFile(pathNodeString, pathProjectString);
}
}
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment