-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement typescript project creation
- Loading branch information
1 parent
187208e
commit 96d78a7
Showing
6 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Location as LocationHelper } from '../helpers/location.helper'; | ||
import { readFileSync, rmSync, writeFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { execSync, spawn } from 'child_process'; | ||
|
||
export const TypescriptProject = { | ||
createApp(name: string) { | ||
if (!name || name.trim().length === 0) { | ||
throw new Error('project name is empty.'); | ||
} | ||
|
||
const location = LocationHelper.toAbsolute(name); | ||
|
||
if (LocationHelper.isExist(location)) { | ||
throw new Error('project name already exists.'); | ||
} | ||
|
||
execSync(`git clone https://github.com/jihyunlab/typescript-starter.git ${name}`, { stdio: 'pipe' }); | ||
|
||
if (!LocationHelper.isExist(location)) { | ||
throw new Error('failed to clone starter project.'); | ||
} | ||
|
||
const basename = LocationHelper.toBasename(location); | ||
|
||
// package.json | ||
const packageJson = JSON.parse(readFileSync(join(location, 'package.json')).toString()); | ||
|
||
packageJson['name'] = basename; | ||
packageJson['description'] = ''; | ||
packageJson['license'] = 'UNLICENSED'; | ||
|
||
delete packageJson['author']; | ||
delete packageJson['homepage']; | ||
delete packageJson['repository']; | ||
delete packageJson['bugs']; | ||
|
||
packageJson['keywords'] = []; | ||
|
||
writeFileSync(join(location, 'package.json'), JSON.stringify(packageJson, null, 2)); | ||
|
||
// README.md | ||
writeFileSync(join(location, 'README.md'), `# ${basename}`); | ||
|
||
// LICENSE | ||
rmSync(join(location, 'LICENSE'), { recursive: true, force: true }); | ||
|
||
// package-lock.json | ||
rmSync(join(location, 'package-lock.json'), { recursive: true, force: true }); | ||
|
||
// .git | ||
rmSync(join(location, '.git'), { recursive: true, force: true }); | ||
|
||
// npm i | ||
const install = spawn('npm', ['i'], { | ||
cwd: location, | ||
env: process.env, | ||
shell: process.platform === 'win32', | ||
stdio: [null, process.stdout, process.stderr], | ||
}); | ||
|
||
install.on('close', () => {}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export const Location = { | ||
isAbsolute(location: string) { | ||
return path.isAbsolute(location); | ||
}, | ||
|
||
toAbsolute(location: string) { | ||
let absolute: string; | ||
|
||
if (this.isAbsolute(location)) { | ||
absolute = location; | ||
} else { | ||
absolute = path.join(process.cwd(), location); | ||
} | ||
|
||
return path.normalize(absolute); | ||
}, | ||
|
||
toRelative(location: string) { | ||
const absolute = this.toAbsolute(location); | ||
const relative = path.relative(process.cwd(), absolute); | ||
|
||
return path.normalize(relative); | ||
}, | ||
|
||
toBasename(location: string) { | ||
return path.basename(this.toAbsolute(location)); | ||
}, | ||
|
||
isExist(location: string) { | ||
return fs.existsSync(location); | ||
}, | ||
|
||
isDirectory(location: string) { | ||
if (!this.isExist(location)) { | ||
return undefined; | ||
} | ||
|
||
return fs.lstatSync(location).isDirectory(); | ||
}, | ||
|
||
toDirectory(location: string, file = false) { | ||
let dirname: string; | ||
|
||
if (file) { | ||
dirname = this.toAbsolute(path.dirname(location)); | ||
} else { | ||
dirname = this.toAbsolute(location); | ||
} | ||
|
||
return path.normalize(dirname); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,71 @@ | ||
export function main() { | ||
return true; | ||
} | ||
#!/usr/bin/env node | ||
import { Command } from 'commander'; | ||
import { TypescriptProject as TypescriptProjectCreator } from './creators/typescript-project.creator'; | ||
import { Location as LocationHelper } from './helpers/location.helper'; | ||
import prompts, { InitialReturnValue } from 'prompts'; | ||
import { setMaxListeners } from 'events'; | ||
|
||
main(); | ||
setMaxListeners(36); | ||
|
||
const handleSigTerm = () => process.exit(0); | ||
|
||
process.on('SIGINT', handleSigTerm); | ||
process.on('SIGTERM', handleSigTerm); | ||
|
||
const onPromptState = (state: { value: InitialReturnValue; aborted: boolean; exited: boolean }) => { | ||
if (state.aborted) { | ||
process.stdout.write('\x1B[?25h'); | ||
process.stdout.write('\n'); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
const program = new Command('create-typescript-app') | ||
.description('JihyunLab Create typescript app') | ||
.addHelpText('after', ' ') | ||
.addHelpText('after', 'Usage example:') | ||
.addHelpText('after', ' npx @jihyunlab/create-typescript-app@latest') | ||
.version('1.0.0'); | ||
|
||
program.action(async () => { | ||
console.log(''); | ||
|
||
const response = await prompts({ | ||
onState: onPromptState, | ||
type: 'text', | ||
name: 'path', | ||
message: 'What is your project name?', | ||
initial: 'my-app', | ||
validate: (name) => { | ||
if (!name || name.trim().length === 0) { | ||
console.log(''); | ||
console.log('error: project name is empty.'); | ||
process.exit(1); | ||
} | ||
|
||
const location = LocationHelper.toAbsolute(name); | ||
|
||
if (LocationHelper.isExist(location)) { | ||
console.log(''); | ||
console.log('error: project name already exists.'); | ||
process.exit(1); | ||
} | ||
|
||
return true; | ||
}, | ||
}); | ||
|
||
try { | ||
TypescriptProjectCreator.createApp(response.path.trim()); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
console.log(`error: ${error.message}`); | ||
} else { | ||
console.log(error); | ||
} | ||
|
||
process.exit(1); | ||
} | ||
}); | ||
|
||
program.parse(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import { main } from '../bin/index'; | ||
|
||
describe('Index', () => { | ||
beforeEach(() => {}); | ||
|
||
afterEach(() => {}); | ||
|
||
test('main()', () => { | ||
expect(main()).toBe(true); | ||
expect(true).toBe(true); | ||
}); | ||
}); |