create-snippet
Version:
npx tool for generating code snippets
127 lines (109 loc) • 4.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModuleConfig = void 0;
const chalk_1 = __importDefault(require("chalk"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const constants_1 = require("../../constants");
const enums_1 = require("../../enums");
class ModuleConfig {
rootDirConfig;
isRootDirConfig;
constructor() {
this.rootDirConfig = constants_1.CONSTANTS.ROOT_DIR_CONFIG;
this.isRootDirConfig = fs_1.default.existsSync(constants_1.CONSTANTS.ROOT_DIR_CONFIG);
}
example(index) {
function objectToString(object) {
const values = Object.values(object);
const indexLastValue = values.length - 1;
return values
.map((item, i) => `'${item.replace(',', '')}'[x]${i !== indexLastValue ? '\n' : ''}`)
.toString()
.replaceAll(',', '')
.replaceAll('[x]', ',');
}
if (index === 0)
return `export const SnippetName = [
${objectToString(enums_1.enumSnippetName)}
]
`;
if (index === 1)
return `export const PrefixNameSnippetName = [
${objectToString(enums_1.enumPrefixName)}
${objectToString(enums_1.enumSnippetName)}
]`;
if (index === 2)
return `export const SnippetNameSuffixName = [
${objectToString(enums_1.enumSnippetName)}
${objectToString(enums_1.enumSuffixName)}
]`;
if (index === 3)
return `export const PrefixNameSnippetNameSuffixName = [
${objectToString(enums_1.enumPrefixName)}
${objectToString(enums_1.enumSnippetName)}
${objectToString(enums_1.enumSuffixName)}
]`;
return `export const SnippetName = [
${objectToString(enums_1.enumSnippetName)}
]
`;
}
init() {
const array = Array.from(Array(4).keys());
const examplePaths = array.map((_, index) => `${this.rootDirConfig}/my-snippet-${index + 1}`);
examplePaths.forEach((pathExample, index) => {
fs_1.default.mkdirSync(path_1.default.join(pathExample), { recursive: true });
fs_1.default.writeFileSync(path_1.default.join(...[pathExample, 'snippet-name.ts']), this.example(index));
});
// eslint-disable-next-line no-console
console.log(`${chalk_1.default.green(enums_1.enumSymbol.check)} ${chalk_1.default.gray(`the project has been successfully initialized`)}`);
}
get() {
const snippets = fs_1.default.readdirSync(this.rootDirConfig);
return snippets.map((snippetName) => ({
snippetName,
pathToSnippet: path_1.default.join(...[this.rootDirConfig, snippetName]),
}));
}
help() {
// eslint-disable-next-line no-console
console.log(`
Documentation
npx create-snippet --help or -h will show hints
npx create-snippet --init or -i initializes the project
npx create-snippet --generate or -g generates a new snippet
npx create-snippet --clear-cache or -cc clear cache
New snippet
When creating a new snippet, create a directory inside the
directory .create-snippet with an arbitrary name,
create the required number of files and directories inside this directory.
SnippetName
The strings [snippetName] specified inside the file or in the file name
will be converted to a custom string when generating a new snippet.
PrefixName
The strings [prefixName] specified inside the file when generating a new
snippet will be converted to custom strings.
SuffixName
The strings [suffixName] specified inside the file when generating a new
snippet will be converted to custom strings.
Supported cases for strings
1) camelCase
2) PascalCase
3) lower_snake_case
4) UPPER_SNAKE_CASE
5) lower-kebab-case
6) UPPER-KEBAB-CASE
Specifying parameters via the console
1) snippet - any string
2) name - any string in the format lower-kebab-case
3) path - any string
4) prefix - any string in the format lower-kebab-case
4) suffix - any string in the format lower-kebab-case
`);
}
}
exports.ModuleConfig = ModuleConfig;