UNPKG

@sinotron/cli

Version:

Simple framework for Typescript Electron projects

113 lines (110 loc) 4.11 kB
import { Dirs } from '../../constants.js'; import { logError, logInfo } from '../../utils/log.js'; import { _join, DirRep, FileRep, fsutil, fsx } from '@utilis/fs'; import chalk from 'chalk'; import { DirFactory } from './DirFactory.js'; import { IpcDirSetup } from './IpcDirSetup.js'; import { DependencyRequest, PackageManagerFactory } from '@utilis/package-manager'; import { fn } from '../../utils/util.js'; import { ask } from '@utilis/cli'; export class FrameworkSetup { constructor() { } static async run(opts = {}) { const { force = false } = opts; const frameworkDir = Dirs.framework(); const projectDir = process.cwd(); if (fsutil.isDir(frameworkDir)) { if (force) { // const resultId = 'answer'; const resultId = await ask.input(`The framework directory already exists. Do you want to overwrite it? (y/n)`); const answer = resultId || ''; if (['y', 'yes'].includes(answer.toLowerCase())) { fsutil.dir.delete(frameworkDir); } } else { logError(`${frameworkDir} already exists. Run with --force option to overwrite it.`); return; } } /* dependencies */ fn(() => { const dependencies = [ '@sinotron/core', 'react-router', 'react-router-dom', '@utilis/env', '@creo-ui/react' ]; logInfo(`-> Installing [${dependencies.join(', ')}]...`); const pm = PackageManagerFactory.forPackageRoot(projectDir); const request = DependencyRequest.new(projectDir).deps(dependencies); pm.install(request); }); // /* react router routes.tsx file */ // fn(() => { // logInfo('--> Setting up __routes__.tsx file for react-router'); // const contents = CodeSources.read(CodeSourceId.routes_tsx_code); // const dir = DirRep.withParent(projectDir, 'src').ensure(); // new FileRep({ // dir: dir.dirPath, // filename: '__routes__.tsx' // }).write(contents); // }); /* reset App.tsx, App.css */ fn(() => { const srcDir = DirRep.withParent(projectDir, 'src'); fn(() => { const appTsxNames = ['App.tsx', 'app.tsx']; for (let name of appTsxNames) { const appTsxFile = FileRep.with(srcDir.dirPath, name); if (appTsxFile.exists()) { // reset file contents appTsxFile.write(`import './App.css' function App() { return ( <> <h3>App</h3> <button onClick={() => { alert('OK') }}>OK</button> </> ) } export default App `); } } }); /* reset App.css */ fn(() => { const appCssNames = ['App.css', 'app.css']; for (let name of appCssNames) { const appCssFile = FileRep.with(srcDir.dirPath, name); if (appCssFile.exists()) { // reset file contents appCssFile.write(`#root { max-width: 1280px; margin: 0 auto; text-align: center; }`); } } FileRep.with(srcDir.dirPath, 'App.css'); }); /* remove assets/react.svg */ fn(() => { if (!fsutil.isDir(_join(srcDir.dirPath, 'assets'))) { return; } fsx.unlinkSync(_join(srcDir.dirPath, 'assets/react.svg')); }); }); /* framework structure */ fn(() => { logInfo(`--> Adding framework contents to ${chalk.yellowBright(projectDir)}`); DirFactory.create(frameworkDir, () => { DirFactory.create(Dirs.db()); IpcDirSetup.run(); }); }); } }