From 46534ed1d20e4efa17b731c39bf3ccb2e3388ad4 Mon Sep 17 00:00:00 2001 From: Ian Dela Cruz Date: Wed, 13 May 2020 08:42:42 +0800 Subject: [PATCH] Modify package.json --- .gitignore | 1 + LICENSE | 21 +++++++++++++++++++++ src/index.js | 11 +++++++++++ src/pkg.js | 24 ++++++++++++++++++++++++ tests/index.test.js | 10 ++++++++-- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 LICENSE create mode 100644 src/pkg.js diff --git a/.gitignore b/.gitignore index c8e9d95..46c1aa4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules/ coverage/ lib/ +hello-world .serverless .webpack \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3da0598 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 KDC Software + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/index.js b/src/index.js index 5efc640..c56ca3c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,8 @@ import fs from 'fs'; import fse from 'fs-extra'; import execa from 'execa'; +import pkg from './pkg'; + // const NPM_PACKAGES = [ // '@babel/core', // '@babel/cli', @@ -34,12 +36,21 @@ const run = async ({ packageName }) => { await fse.mkdirp(packageName); process.chdir(packageName); await execa('npm', ['init', '-y']); + pkg.mod([ + { field: 'version', value: '0.1.0' }, + { field: 'main', value: 'lib/index.js' }, + { field: 'license', value: 'MIT' }, + ]); + + // pkg.mod({ field: 'main', value: 'lib/index.js' }); + // console.log('hello'); // init dir // init git // npm install // copy configs // fs.mkdir('mydir'); + // get user from git } catch (error) { // console.log(error); throw new Error(error); diff --git a/src/pkg.js b/src/pkg.js new file mode 100644 index 0000000..f224e82 --- /dev/null +++ b/src/pkg.js @@ -0,0 +1,24 @@ +import fs from 'fs'; + +const PKG_FILE = 'package.json'; + +const read = () => { + const contents = fs.readFileSync(PKG_FILE, 'utf-8'); + return JSON.parse(contents); +}; + +const write = (pkg) => { + fs.writeFileSync(PKG_FILE, JSON.stringify(pkg, null, 2)); +}; + +const mod = (fields) => { + const pkg = read(); + + fields.forEach((f) => { + pkg[f.field] = f.value; + }); + + write(pkg); +}; + +export default { mod }; diff --git a/tests/index.test.js b/tests/index.test.js index 0189e71..a8e4411 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -12,15 +12,21 @@ describe('Create NodeJs', () => { }); afterAll(async () => { - await fse.remove(PKG_DIR); + // await fse.remove(PKG_DIR); }); it('should run', async () => { - expect.assertions(2); + expect.assertions(5); try { await run({ packageName: 'hello-world' }); expect(fs.existsSync(PKG_DIR)).toBe(true); expect(fs.existsSync(`${PKG_DIR}/package.json`)).toBe(true); + + const contents = fs.readFileSync(`${PKG_DIR}/package.json`, 'utf-8'); + const pkg = JSON.parse(contents); + expect(pkg.version).toBe('0.1.0'); + expect(pkg.main).toBe('lib/index.js'); + expect(pkg.license).toBe('MIT'); } catch (error) { // console.log(error); expect(error).toBe(undefined);