Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
initial commit tosin
Browse files Browse the repository at this point in the history
- tosin clone repo and replace values
- create integration test for replacement value method
  • Loading branch information
Ffloriel committed May 27, 2019
1 parent a06448a commit c73b68f
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
__tests__/fixtures/filesReplacement/test.md

# Logs
logs
*.log
Expand Down
15 changes: 15 additions & 0 deletions __tests__/fixtures/filesReplacement/README.md
@@ -0,0 +1,15 @@
# This file contains every values to be replace by the cli

- Project Name = {% project name %}
- Human Project Name = {% human project name %}
- Developer Name = {% developer name %}
- Email = {% email %}
- Repository = {% repository %}
- Lowercase Repository = {% lowercase repository %}

- Project Name = {% project name %}
- Human Project Name = {% human project name %}
- Developer Name = {% developer name %}
- Email = {% email %}
- Repository = {% repository %}
- Lowercase Repository = {% lowercase repository %}
15 changes: 15 additions & 0 deletions __tests__/fixtures/filesReplacement/README_EXPECTED.md
@@ -0,0 +1,15 @@
# This file contains every values to be replace by the cli

- Project Name = tosin-cli
- Human Project Name = Awesome Tosin
- Developer Name = Floriel Fedry
- Email = truc@gmail.com
- Repository = FullHuman/tosin
- Lowercase Repository = fullhuman/tosin

- Project Name = tosin-cli
- Human Project Name = Awesome Tosin
- Developer Name = Floriel Fedry
- Email = truc@gmail.com
- Repository = FullHuman/tosin
- Lowercase Repository = fullhuman/tosin
32 changes: 27 additions & 5 deletions __tests__/index.test.js
@@ -1,9 +1,31 @@
import { sum } from './../src/index'
import fs from 'fs'
import { promisify } from 'util'
import { replaceInFile } from './../src/index'

const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)

describe('test the library', () => {
it('returns the expected result', () => {
const actual = sum(1, 2)
const expected = 3
expect(actual).toBe(expected)

const replaceSets = [
['{% project name %}', 'tosin-cli'],
['{% human project name %}', 'Awesome Tosin'],
['{% developer name %}', 'Floriel Fedry'],
['{% email %}', 'truc@gmail.com'],
['{% repository %}', 'FullHuman/tosin'],
['{% lowercase repository %}', 'fullhuman/tosin']
]

it('replace the text with the values', async () => {
const testFilePath = '__tests__/fixtures/filesReplacement/test.md'

const fileReadme = await readFile('__tests__/fixtures/filesReplacement/README.md', 'utf-8')
await writeFile(testFilePath, fileReadme, 'utf-8')

const fileReadmeExpected = await readFile('__tests__/fixtures/filesReplacement/README_EXPECTED.md', 'utf-8')
await replaceInFile(testFilePath, replaceSets)
const actualFile = await readFile(testFilePath, 'utf-8')

expect(actualFile).toBe(fileReadmeExpected)
})
})
17 changes: 17 additions & 0 deletions bin/tosin.js
@@ -0,0 +1,17 @@
#!/usr/bin/env node

const { init } = require("../lib/tosin.cjs");

const program = require('commander');

program
.version('1.0.0')
.command('init')
.action(() => {
init()
.then(() => process.exit(0))
.catch(() => process.exit(1))
})

program.parse(process.argv);

90 changes: 90 additions & 0 deletions lib/tosin.cjs.js
@@ -0,0 +1,90 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var fs = _interopDefault(require('fs'));
var util = require('util');
var enquirer = _interopDefault(require('enquirer'));
var degit = _interopDefault(require('degit'));

const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);

const init = async () => {
const response = await enquirer.prompt([
{
type: 'input',
name: 'humanProjectName',
message: 'What is the name of your project? (e.g. Svelte)'
},
{
type: 'input',
name: 'projectName',
mesasge: 'What is the npm project name? (e.g. svelte-js)'
},
{
type: 'input',
name: 'repository',
message: 'What is your repository (<username>/<repo>)? (e.g. FullHuman/tosin)'
},
{
type: 'input',
name: 'developerName',
message: 'What is your name (used for the licence)?'
},
{
type: 'input',
name: 'email',
message: 'What is the email to be use for the code of conduct?'
}
]);
// clone the template library
const emmitter = degit('FullHuman/tosin-template-library', {
cache: true,
force: true,
verbose: true
});

emitter.on('info', info => {
console.log(info.message);
});

await emmitter.clone('.');

const replaceSets = [
['{% project name %}', response.projectName],
['{% human project name %}', response.humanProjectName],
['{% developer name %}', response.developerName],
['{% email %}', response.email],
['{% repository %}', response.repository],
['{% lowercase repository %}', response.repository.toLowerCase()]
]

;[
'.github/ISSUE_TEMPLATE/BUG_REPORT.md',
'.github/CODE_OF_CONDUCT.md',
'.github/PULL_REQUEST_TEMPLATE.md',
'scripts/deploy_docs.sh',
'CONTRIBUTING.md',
'LICENSE',
'package.json',
'README.md',
'rollup.config.js'
].forEach(file => {
replaceInFile(file, replaceSets);
});
};

const replaceInFile = async (filePath, replaceSets) => {
const fileContent = await readFile(filePath, 'utf-8');
let result = fileContent;
replaceSets.forEach(([toReplace, value]) => {
result = result.replace(new RegExp(toReplace, 'g'), value);
});
await writeFile(filePath, result, 'utf8');
};

exports.init = init;
exports.replaceInFile = replaceInFile;
83 changes: 83 additions & 0 deletions lib/tosin.es.js
@@ -0,0 +1,83 @@
import fs from 'fs';
import { promisify } from 'util';
import enquirer from 'enquirer';
import degit from 'degit';

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

const init = async () => {
const response = await enquirer.prompt([
{
type: 'input',
name: 'humanProjectName',
message: 'What is the name of your project? (e.g. Svelte)'
},
{
type: 'input',
name: 'projectName',
mesasge: 'What is the npm project name? (e.g. svelte-js)'
},
{
type: 'input',
name: 'repository',
message: 'What is your repository (<username>/<repo>)? (e.g. FullHuman/tosin)'
},
{
type: 'input',
name: 'developerName',
message: 'What is your name (used for the licence)?'
},
{
type: 'input',
name: 'email',
message: 'What is the email to be use for the code of conduct?'
}
]);
// clone the template library
const emmitter = degit('FullHuman/tosin-template-library', {
cache: true,
force: true,
verbose: true
});

emitter.on('info', info => {
console.log(info.message);
});

await emmitter.clone('.');

const replaceSets = [
['{% project name %}', response.projectName],
['{% human project name %}', response.humanProjectName],
['{% developer name %}', response.developerName],
['{% email %}', response.email],
['{% repository %}', response.repository],
['{% lowercase repository %}', response.repository.toLowerCase()]
]

;[
'.github/ISSUE_TEMPLATE/BUG_REPORT.md',
'.github/CODE_OF_CONDUCT.md',
'.github/PULL_REQUEST_TEMPLATE.md',
'scripts/deploy_docs.sh',
'CONTRIBUTING.md',
'LICENSE',
'package.json',
'README.md',
'rollup.config.js'
].forEach(file => {
replaceInFile(file, replaceSets);
});
};

const replaceInFile = async (filePath, replaceSets) => {
const fileContent = await readFile(filePath, 'utf-8');
let result = fileContent;
replaceSets.forEach(([toReplace, value]) => {
result = result.replace(new RegExp(toReplace, 'g'), value);
});
await writeFile(filePath, result, 'utf8');
};

export { init, replaceInFile };
19 changes: 11 additions & 8 deletions package.json
@@ -1,10 +1,10 @@
{
"name": "{% project name %}",
"name": "tosin",
"version": "1.0.0",
"description": "",
"main": "./lib/{% project name %}.js",
"module": "./lib/{% project name %}.es.js",
"jsnext:main": "./lib/{% project name %}.es.js",
"main": "./lib/tosin.js",
"module": "./lib/tosin.es.js",
"jsnext:main": "./lib/tosin.es.js",
"directories": {
"lib": "lib",
"test": "__tests__",
Expand All @@ -21,7 +21,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/{% repository %}.git"
"url": "git+https://github.com/FullHuman/tosin.git"
},
"keywords": [
"github",
Expand All @@ -39,9 +39,9 @@
"author": "Ffloriel",
"license": "MIT",
"bugs": {
"url": "https://github.com/{% repository %}/issues"
"url": "https://github.com/FullHuman/tosin/issues"
},
"homepage": "https://github.com/{% repository %}#readme",
"homepage": "https://github.com/FullHuman/tosin#readme",
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
Expand All @@ -53,7 +53,10 @@
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-resolve": "^5.0.0"
},
"dependencies": {},
"dependencies": {
"degit": "^2.1.3",
"enquirer": "^2.3.0"
},
"engines": {
"node": ">=8.0.0",
"npm": ">=5.2.0"
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.js
Expand Up @@ -5,14 +5,14 @@ export default {
input: "src/index.js",
output: [
{
file: "lib/{% project name %}.es.js",
file: "lib/tosin.es.js",
format: "es"
},
{
file: "lib/{% project name %}.cjs.js",
file: "lib/tosin.cjs.js",
format: "cjs"
}
],
plugins: [builtins(), resolve()],
external: []
external: ['degit', 'enquirer', 'util', 'fs']
}

0 comments on commit c73b68f

Please sign in to comment.