This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Alorel/dev-yarn-proj
Dev yarn proj
- Loading branch information
Showing
12 changed files
with
260 additions
and
66 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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
/.idea/ | ||
/node_modules/ | ||
/dist/ | ||
/coverage/ | ||
/.nyc_output/ | ||
/.tmp/ | ||
/package-lock.json | ||
/yarn-error.log | ||
/*.tgz | ||
/git_gpg_keys.asc | ||
/tmp.ts | ||
/tmp.js | ||
.idea/ | ||
node_modules/ | ||
dist/ | ||
coverage/ | ||
.nyc_output/ | ||
yarn-error.log | ||
*.tgz | ||
git_gpg_keys.asc |
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
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,11 @@ | ||
import {values} from 'lodash'; | ||
|
||
export enum License { | ||
MIT = 'MIT' | ||
} | ||
|
||
export function isLicense(v: any): v is License { | ||
return v === License.MIT; | ||
} | ||
|
||
export const LICENSE_VALUES: string[] = <string[]>Object.freeze(values(License)); |
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,10 @@ | ||
import {HasEmail, HasName, HasUserWebsite} from '../commons/identity'; | ||
import {License} from '../inc/License'; | ||
|
||
export interface InitConf extends HasName, HasUserWebsite, HasEmail { | ||
license: License; | ||
|
||
skipGitignore: boolean; | ||
|
||
skipLicense: boolean; | ||
} |
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,54 @@ | ||
import * as fs from 'fs'; | ||
|
||
function trim(s: string): string { | ||
return s.trim(); | ||
} | ||
|
||
export class LineReadWriter { | ||
private readonly lines: string[]; | ||
|
||
private constructor(contents?: string, private readonly file?: string) { | ||
this.lines = contents && (contents = contents.trim()) ? contents.split(/\n/g).map(trim) : []; | ||
} | ||
|
||
public static createFromContents(contents: string): LineReadWriter { | ||
return new LineReadWriter(contents); | ||
} | ||
|
||
public static createFromFile(filepath: string): LineReadWriter { | ||
try { | ||
return new LineReadWriter(fs.readFileSync(filepath, 'utf8'), filepath); | ||
} catch { | ||
return new LineReadWriter(undefined, filepath); | ||
} | ||
} | ||
|
||
public ensure(...lines: string[]): this { | ||
const toAdd = new Set<string>(); | ||
for (const line of lines) { | ||
if (!this.lines.includes(line)) { | ||
toAdd.add(line); | ||
} | ||
} | ||
|
||
if (toAdd.size) { | ||
this.lines.push(...toAdd); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public save(): this { | ||
if (!this.file) { | ||
throw new Error('File not provided'); | ||
} | ||
|
||
fs.writeFileSync(this.file, this.toString()); | ||
|
||
return this; | ||
} | ||
|
||
public toString(): string { | ||
return this.lines.join('\n') + '\n'; | ||
} | ||
} |
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,20 @@ | ||
import {InitConf} from '../../interfaces/InitConf'; | ||
import {LineReadWriter} from '../LineReadWriter'; | ||
import {PromptableConfig} from '../PromptableConfig'; | ||
|
||
export function initGitignore(c: PromptableConfig<InitConf>): void { | ||
if (!c.get('skipGitignore')) { | ||
LineReadWriter.createFromFile('.gitignore') | ||
.ensure( | ||
'.idea/', | ||
'node_modules/', | ||
'dist/', | ||
'coverage/', | ||
'.nyc_output/', | ||
'yarn-error.log', | ||
'*.tgz', | ||
'git_gpg_keys.asc' | ||
) | ||
.save(); | ||
} | ||
} |
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,25 @@ | ||
import {isLicense, LICENSE_VALUES} from '../../inc/License'; | ||
import {InitConf} from '../../interfaces/InitConf'; | ||
import {LicenseTpl} from '../../interfaces/LicenseTpl'; | ||
import {Fixture} from '../Fixture'; | ||
import {PromptableConfig} from '../PromptableConfig'; | ||
|
||
export function initLicense(c: PromptableConfig<InitConf>): void { | ||
if (!c.get('skipLicense')) { | ||
if (!isLicense(c.get('license'))) { | ||
throw new Error('Invalid license'); | ||
} | ||
|
||
new Fixture('init/license') | ||
.template<LicenseTpl>( | ||
`${c.getPromptSelect('license', 'What license do you want to use? ', LICENSE_VALUES)}.txt`, | ||
{ | ||
email: c.getPromptEmail('email', 'What\'s your email? '), | ||
name: c.getPrompt('name', 'What\'s your name? '), | ||
url: c.getPrompt('userWebsite', 'What\'s your url? '), | ||
year: new Date().getFullYear() | ||
}, | ||
'LICENSE' | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {expect} from 'chai'; | ||
import * as fs from 'fs-extra'; | ||
import {LineReadWriter} from '../../src/lib/LineReadWriter'; | ||
import {tmpFile} from '../util/tmp-test'; | ||
|
||
describe('LineReadWriter', () => { | ||
let contents: string; | ||
let lines: string[]; | ||
|
||
before('init contents', async () => { | ||
contents = (await fs.readFile(__filename, 'utf8')).trim() | ||
.split(/\n/g) | ||
.map(l => l.trim()) | ||
.join('\n'); | ||
}); | ||
|
||
before('init lines', () => { | ||
lines = contents.split(/\n/g).map(l => l.trim()); | ||
}); | ||
|
||
it('from contents', () => { | ||
expect(LineReadWriter.createFromContents(contents)['lines']) | ||
.to.deep.eq(lines); | ||
}); | ||
|
||
it('from file', () => { | ||
expect(LineReadWriter.createFromFile(__filename)['lines']) | ||
.to.deep.eq(lines); | ||
}); | ||
|
||
it('toString()', () => { | ||
expect(LineReadWriter.createFromContents(contents).toString()) | ||
.to.eq(`${contents}\n`); | ||
}); | ||
|
||
it('ensure', () => { | ||
const lrw = LineReadWriter.createFromContents('') | ||
.ensure('foo', 'foo', 'bar'); | ||
|
||
expect(lrw['lines']).to.deep.eq(['foo', 'bar']); | ||
}); | ||
|
||
describe('save', () => { | ||
let p: string; | ||
|
||
before('write', async () => { | ||
p = tmpFile(); | ||
await fs.writeFile(p, 'qux\nbaz\n'); | ||
LineReadWriter.createFromFile(p) | ||
.ensure('foo') | ||
.save(); | ||
}); | ||
|
||
it('', async () => { | ||
expect(await fs.readFile(p, 'utf8')).to.eq('qux\nbaz\nfoo\n'); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.