You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

76 lines
2.4 KiB

#!/usr/bin/env node
import prompts from 'prompts';
import kebabCase from 'lodash.kebabcase';
import sanitize from 'sanitize-filename';
import util from 'util';
import fs from 'fs-extra';
import path from 'path';
import { sortPackageJson } from 'sort-package-json';
import { exec } from 'child_process';
const execute = util.promisify(exec);
const repoUrls = {
js: 'https://git.tinkr.site/jeffi/js-tinkrplugin-boilerplate.git',
ts: 'https://git.tinkr.site/jeffi/ts-tinkr-plugin-boilerplate.git',
};
const invalidName = `
It is recommended to keep your plugin name short.
`.trim();
const invalidVersion = `
A valid version has one to four dot-separated integers.
Examples: "1", "1.0", "2.0.3", "3.4.7.219"
`.trim();
prompts([
{
type: 'text',
name: 'name',
message: 'Tinkr plugin package name:',
validate: (n) => n.length < 32 || invalidName,
},
{
type: 'text',
name: 'version',
message: 'First version number:',
initial: '1.0.0',
validate: (v) => !/[^.\d]/.test(v) || invalidVersion,
},
{
type: 'text',
name: 'author',
message: 'Author name:',
},
{
type: 'text',
name: 'description',
message: 'Description:',
},
{
type: 'select',
name: 'lang',
message: 'Which do you want to use?',
choices: [
{ title: 'JavaScript', value: 'js' },
{ title: 'TypeScript', value: 'ts' },
],
initial: 0,
},
]).then(async ({ name, version, author, description, lang }) => {
const packageName = kebabCase(sanitize(name, { replacement: '-' }));
console.log(`Creating a Tinkr plugin in ./${packageName}`);
await execute(`git clone ${repoUrls[lang]} ${packageName}`);
const packageDir = path.join(process.cwd(), packageName);
const packageJsonPath = path.join(packageDir, 'package.json');
const { bugs, homepage, keywords, license, repository, ...packageJson } = await fs.readJSON(packageJsonPath);
await fs.writeJSON(packageJsonPath, sortPackageJson({
...packageJson,
author,
description,
name: packageName,
version,
}), { spaces: 2 });
const gitFolderPath = path.join(packageDir, '.git');
await fs.remove(gitFolderPath);
console.log('Success: Now just `npm install` using your favorite package manager and create your Tinkr plugin!');
}).catch((error) => {
console.error(error);
process.exit(1);
});