74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import path from 'path';
|
|
import fs from 'fs-extra';
|
|
|
|
import { NPMExecutor } from '@squeebot/core/lib/npm';
|
|
|
|
import { defaultEnvironment } from '../default/environment.default';
|
|
import { gitignore } from '../default/gitignore.default';
|
|
import { tsConfig } from '../default/tsconfig.default';
|
|
import { REPOSITORY_SCHEMA } from './constants';
|
|
|
|
/**
|
|
* Create a new repository, complete with typescript and git.
|
|
* @param name Repository name
|
|
* @param location Repository path
|
|
* @param typescript Use typescript
|
|
*/
|
|
export async function newRepository(
|
|
name: string,
|
|
location?: string,
|
|
typescript = true): Promise<void> {
|
|
if (!name) {
|
|
throw new Error('Repository needs a name!');
|
|
}
|
|
|
|
if (!location) {
|
|
location = name;
|
|
}
|
|
|
|
location = path.resolve(process.cwd(), location);
|
|
console.log('Creating a new repository development environment at', location);
|
|
|
|
await fs.ensureDir(location);
|
|
const env = defaultEnvironment(location);
|
|
|
|
console.log('Creating package.json and installing base requirements');
|
|
const executor = new NPMExecutor(env, '@squeebot/core');
|
|
await executor.loadPackageFile();
|
|
|
|
let gitIgnore = gitignore + '';
|
|
|
|
if (typescript) {
|
|
console.log('Installing TypeScript support');
|
|
await executor.installPackage('typescript');
|
|
await fs.writeJson(path.join(location, 'tsconfig.json'), tsConfig);
|
|
gitIgnore += '\n*.js';
|
|
gitIgnore += '\n!.eslintrc.js';
|
|
gitIgnore += '\n*.d.ts';
|
|
gitIgnore += '\n*.tsbuildinfo';
|
|
gitIgnore += '\n';
|
|
|
|
console.log('Adding TypeScript scripts to package.json');
|
|
const pkgjson = path.join(location, 'package.json');
|
|
const contents = await fs.readJson(pkgjson);
|
|
contents.scripts = {
|
|
build: 'tsc',
|
|
watch: 'tsc -w',
|
|
};
|
|
await fs.writeJson(pkgjson, contents, { spaces: 2 });
|
|
}
|
|
|
|
console.log('Creating .gitignore');
|
|
await fs.writeFile(path.join(location, '.gitignore'), gitIgnore);
|
|
|
|
console.log('Writing build metadata');
|
|
await fs.writeJson(path.join(location, 'squeebot.repo.json'), {
|
|
name,
|
|
plugins: [],
|
|
typescript,
|
|
$schema: REPOSITORY_SCHEMA,
|
|
}, { spaces: 2 });
|
|
|
|
console.log('\nDone! Your repository "%s" lives at:', name, location);
|
|
}
|