Skip to content
Open
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
@@ -1,5 +1,5 @@
# Create-Staffbase-SSO-CLI Documentation
![Staffbase Logo](https://staffbase.com/wp-content/themes/staffbase-theme/img/logo-blau.svg)
<img src="docs/assets/images/staffbase.png" alt="Staffbase SE" width="96" />

## Getting started
Install the package in your global namespace and then run the command.
Expand Down Expand Up @@ -83,7 +83,7 @@ Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/
<table>
<tr>
<td>
<img src="docs/assets/images/staffbase.png" alt="Staffbase GmbH" width="96" />
<img src="docs/assets/images/staffbase.png" alt="Staffbase SE" width="96" />
</td>
<td>
<b>Staffbase SE</b>
Expand Down
239 changes: 120 additions & 119 deletions csss.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
#! /usr/bin/env node
'use strict';
const fs = require('fs-extra');
const path = require('path');
const yargv = require('yargs');
const validateNPM = require('validate-npm-package-name');
const execFile = require('child_process').execFile;
const prompt = require('prompt');
const filepath = require('filepath');
const validatePath = require('./lib/helpers').validatePath;
const isRelativePath = require('is-relative');
const colors = require('colors/safe');
"use strict";
const fs = require("fs-extra");
const path = require("path");
const yargs = require("yargs");
const validateNPM = require("validate-npm-package-name");
const execFile = require("child_process").execFile;
const prompt = require("prompt");
const filepath = require("filepath");
const validatePath = require("./lib/helpers").validatePath;
const isRelativePath = require("is-relative");
const colors = require("colors/safe");

// configure prompt
prompt.message = '';
prompt.delimiter = colors.green(':');
prompt.message = "";
prompt.delimiter = colors.green(":");
// set default values
const defaultNPMName = 'my-staffbase-backend';
const scaffoldFolder = path.resolve(__dirname, './scaffoldTpl');
const defaultNPMName = "my-staffbase-backend";
const scaffoldFolder = path.resolve(__dirname, "./scaffoldTpl");

yargv
.usage('Usage: create-staffbase-sso-server <project-directory> [Options]')
.alias('name', 'N')
.string('name')
.describe('name', 'a sepcific package.json name of your app')
.version('0.0.1')
.help('help')
.epilogue(`for more information,\please see the README at:
https://github.com/Staffbase/create-staffbase-plugin-nodejs/blob/main/README.MD`);
// console.log('YARGS Parsed Data:\n', yargv.argv);
const packageJSON = fs.readJSONSync(path.join(scaffoldFolder, 'package.json'));
const yargv = yargs(process.argv.slice(2))
.usage("Usage: create-staffbase-sso-server <project-directory> [Options]")
.alias("name", "N")
.string("name")
.describe("name", "a specific package.json name of your app")
.version("0.0.1")
.help("help")
.epilogue(`for more information,\please see the README at:
https://github.com/Staffbase/create-staffbase-plugin-nodejs/blob/main/README.MD`)
.argv;
// console.log('YARGS Parsed Data:\n', yargv);
const packageJSON = fs.readJSONSync(path.join(scaffoldFolder, "package.json"));
// Defaults package name to current folder name
const nameParam = yargv.argv.N || yargv.argv.name || defaultNPMName;
const nameParam = yargv.N || yargv.name || defaultNPMName;
prompt.override = {
name: yargv.argv.N,
path: yargv.argv._[0],
name: yargv.N,
path: yargv._[0],
};
/**
* Prompts user for just the npm name value.
Expand All @@ -44,25 +45,24 @@ function promptName(name) {
const namePromptSchema = {
properties: {
name: {
description: 'What is the npm name of your plugin?',
type: 'string',
description: "What is the npm name of your plugin?",
type: "string",
default: name,
message: 'Name must be npm.js compatible',
message: "Name must be npm.js compatible",
required: true,
conform: function(value) {
conform: function (value) {
return validateNPM(value).validForNewPackages;
},
},
},
};
return new Promise(function(resolve, reject) {
prompt.start()
.get(namePromptSchema, function(err, res) {
if (err) {
return reject(err);
}
return resolve(res);
});
return new Promise(function (resolve, reject) {
prompt.start().get(namePromptSchema, function (err, res) {
if (err) {
return reject(err);
}
return resolve(res);
});
});
}
/**
Expand All @@ -75,33 +75,34 @@ function promptPath(promtedName) {
const pathPromptSchema = {
properties: {
path: {
description: 'Please enter the folder path for the App',
type: 'string',
description: "Please enter the folder path for the new plugin project",
type: "string",
message:
'Entered path is invalid or an already present file on the File System. Please enter a correct filepath,',
"Entered path is invalid or an already present file on the File System. Please enter a correct filepath.",
default: defPath,
required: true,
conform: validatePath,
},
override: {
message:
colors.yellow('The directory you specified already exists. It directory will be overridden!') +
'\nDo you wish to proceed (y)es|(n)o?',
colors.yellow(
"The directory you specified already exists. The directory will be overridden!"
) + "\nDo you wish to proceed (y)es|(n)o?",
validator: /y[es]*|n[o]?/,
warning: 'Must respond yes or no',
default: 'yes',
ask: function() {
warning: "Must respond yes or no",
default: "yes",
ask: function () {
let chkPath = defPath;
if (prompt.history('path')) {
chkPath = prompt.history('path').value;
if (prompt.history("path")) {
chkPath = prompt.history("path").value;
}
return filepath.create(chkPath).exists();
},
},
},
};
return new Promise(function(resolve, reject) {
prompt.get(pathPromptSchema, function(err, res) {
return new Promise(function (resolve, reject) {
prompt.get(pathPromptSchema, function (err, res) {
if (err) {
return reject(err);
}
Expand All @@ -111,33 +112,32 @@ function promptPath(promtedName) {
}

/**
* Copy contants from the Scaffold Template to the specified folder
* @param {String} dstDir THe destination directory where files are to be copied
* Copy contents from the Scaffold Template to the specified folder
* @param {String} dstDir The destination directory where files are to be copied
* @return {Promise} Promise resolved when the copy process is complete. Rejected
* if there is some error in copying files.
*/
function copyContents(dstDir) {
// console.log(colors.blue('Copying from:' + path.resolve(__dirname, './scaffoldTpl')));
const scaffoldFolder = path.resolve(__dirname, './scaffoldTpl');
const scaffoldFolder = path.resolve(__dirname, "./scaffoldTpl");
return fs.copy(scaffoldFolder, dstDir);
}
/**
* Repace the package.json file from copied fromplate to the new generated one.
* Replace the package.json file from copied template to the new generated one.
* @return {Promise} Promise resolved when the Package.json is successfully replaced.
* @param {String} dstPath The path of the folder where the package.json needs to be replaced
* @param {String} nameVal name value that should be replaced
* Rejected if there is some error in creating new Package.json file.
*/
function replacePackageJSON(dstPath, nameVal) {
// console.log("replacePackageJSON");
const newPackageJSON = Object.assign({}, packageJSON, {name: nameVal});
const newPackageJSON = Object.assign({}, packageJSON, { name: nameVal });
const curDir = path.resolve(dstPath);
const packagePath = path.resolve(path.join(curDir, 'package.json'));
return fs.remove(packagePath)
.then(function(data) {
// console.log(colors.yellow('Writing json...'));
return fs.writeJson(packagePath, newPackageJSON, {spaces: 2});
});
const packagePath = path.resolve(path.join(curDir, "package.json"));
return fs.remove(packagePath).then(function (data) {
// console.log(colors.yellow('Writing json...'));
return fs.writeJson(packagePath, newPackageJSON, { spaces: 2 });
});
}
/**
* Installs the node modules in the folder where the template was created.
Expand All @@ -146,12 +146,12 @@ function replacePackageJSON(dstPath, nameVal) {
* @return {Promise} Promise resolved when the packages are successfulyl installed.
*/
function installDeps(dstPath) {
console.log(colors.italic('\nInstalling dependencies...'));
console.log(colors.italic("\nInstalling dependencies..."));
const opts = {
cwd: path.resolve(dstPath),
};
return new Promise( (resolve, reject) => {
execFile('npm', ['install'], opts, (err, stdout, stderr) => {
return new Promise((resolve, reject) => {
execFile("npm", ["install"], opts, (err, stdout, stderr) => {
// console.log(colors.red('Inside Child result', stderr, stdout, err));
if (err) {
// console.log(colors.red(err));
Expand All @@ -170,11 +170,10 @@ function installDeps(dstPath) {
function removeExistingFolder(dstPath) {
const fp = filepath.create(dstPath);
if (fp.exists()) {
return fs.remove(fp.toString())
.then(function() {
console.log(colors.red('Removing existing folder and its contents...'));
return dstPath;
});
return fs.remove(fp.toString()).then(function () {
console.log(colors.red("Removing existing folder and its contents..."));
return dstPath;
});
} else {
return Promise.resolve(dstPath);
}
Expand All @@ -183,56 +182,58 @@ function removeExistingFolder(dstPath) {
const promptRes = {};
// promot package name
promptName(nameParam)
// prompt file path
.then(function(pathResp) {
const nameRecv = pathResp.name;
Object.assign(promptRes, pathResp);
return promptPath(nameRecv);
})
// remove the folder if it exists
.then(function(pathResp) {
if (pathResp.override === 'n' || pathResp.override === 'no') {
return Promise.reject(console.log(colors.green('Good Bye!')));
}
Object.assign(promptRes, pathResp);
let pathRecv = pathResp.path;
// if the entered path is relative, resolve to absolute
if (isRelativePath(pathRecv)) {
pathRecv = path.resolve(path.join(process.cwd(), pathRecv));
promptRes.path = pathRecv;
}
return removeExistingFolder(pathRecv);
})
// copy contents to folder
.then((pathRecv) => {
return (copyContents(pathRecv));
})
// replace package.json with new one
.then((res) => {
return replacePackageJSON(promptRes.path, promptRes.name);
})
// install npm dependencies
.then((res) => {
return installDeps(promptRes.path);
})
// output end results
.then(function(npmOutput) {
console.log(colors.yellow(npmOutput));
console.log(colors.green(`
// prompt file path
.then(function (pathResp) {
const nameRecv = pathResp.name;
Object.assign(promptRes, pathResp);
return promptPath(nameRecv);
})
// remove the folder if it exists
.then(function (pathResp) {
if (pathResp.override === "n" || pathResp.override === "no") {
return Promise.reject(console.log(colors.green("Good Bye!")));
}
Object.assign(promptRes, pathResp);
let pathRecv = pathResp.path;
// if the entered path is relative, resolve to absolute
if (isRelativePath(pathRecv)) {
pathRecv = path.resolve(path.join(process.cwd(), pathRecv));
promptRes.path = pathRecv;
}
return removeExistingFolder(pathRecv);
})
// copy contents to folder
.then((pathRecv) => {
return copyContents(pathRecv);
})
// replace package.json with new one
.then((res) => {
return replacePackageJSON(promptRes.path, promptRes.name);
})
// install npm dependencies
.then((res) => {
return installDeps(promptRes.path);
})
// output end results
.then(function (npmOutput) {
console.log(colors.yellow(npmOutput));
console.log(
colors.green(`
Your application setup is complete!
Please see the generated README.MD file to get more details about next steps.
You can find your application template in: ${promptRes.path}.
`));
})
// handle errors if any
.catch(function(err) {
if (err.message === 'canceled') {
return console.log(colors.green('\nGood Bye!'));
}
if (err.message) {
console.log('An error occured.', err);
}
});
`)
);
})
// handle errors if any
.catch(function (err) {
if (err.message === "canceled") {
return console.log(colors.green("\nGood Bye!"));
}
if (err.message) {
console.log("An error occurred.", err);
}
});

module.exports = {
validatePath: validatePath,
Expand Down
12 changes: 6 additions & 6 deletions csss.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const validatePath = require('./lib/helpers').validatePath;
const validatePath = require("./lib/helpers").validatePath;

const validPath = './scaffoldTpl';
const inValidPath = '*/,js';
const validPath = "./scaffoldTpl";
const inValidPath = "*/,js";

describe('Testing csss.validatePath', () => {
test('test validating existing path', () => {
describe("Testing csss.validatePath", () => {
test("test validating existing path", () => {
expect(validatePath(validPath)).toBe(true);
});
test('test validating invalid path', () => {
test("test validating invalid path", () => {
expect(validatePath(inValidPath)).toBe(false);
});
});
6 changes: 3 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const isValidPath = require('is-valid-path');
const filepath = require('filepath');
const isValidPath = require("is-valid-path");
const filepath = require("filepath");
/**
* Checks if the path specified is valid for setting up the template.
* Valid means if the path is a valid path and it is not a File (not a folder).
Expand All @@ -13,7 +13,7 @@ function validatePath(path) {
try {
const fp = filepath.create(path);
// console.log(colors.yellow(fp));
return !(fp.isFile());
return !fp.isFile();
} catch (err) {
console.log(err);
}
Expand Down
Loading