Skip to content

Commit

Permalink
fix(init): init package with better main, types (#179)
Browse files Browse the repository at this point in the history
Fixes: #144
  • Loading branch information
ofrobots committed Jun 26, 2018
1 parent 4f09fc3 commit 122fe81
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/init.ts
Expand Up @@ -21,7 +21,19 @@ import * as path from 'path';
import {Options} from './cli';
import {readFilep as read, readJsonp as readJson, writeFileAtomicp as write} from './util';

const pkg = require('../../package.json') as PackageJson;
const pkg = require('../../package.json');

const DEFUALT_PACKAGE_JSON: PackageJson = {
name: '',
version: '0.0.0',
description: '',
main: 'build/src/index.js',
types: 'build/src/index.d.ts',
files: ['build/src'],
license: 'Apache-2.0',
keywords: [],
scripts: {test: 'echo "Error: no test specified" && exit 1'}
};

export interface Bag<T> {
[script: string]: T;
Expand All @@ -33,6 +45,13 @@ export interface PackageJson {
version?: string;
devDependencies?: Bag<string>;
scripts?: Bag<string>;
name: string;
description: string;
main: string;
types: string;
files: string[];
license: string;
keywords: string[];
}

async function query(
Expand Down Expand Up @@ -193,6 +212,7 @@ async function generateConfigFile(
}

export async function init(options: Options): Promise<boolean> {
let generatedPackageJson = false;
let packageJson;
try {
packageJson = await readJson('./package.json');
Expand All @@ -209,18 +229,13 @@ export async function init(options: Options): Promise<boolean> {
return false;
}

try {
// TODO(ofrobots): add proper error handling.
cp.spawnSync('npm', ['init', '-y']);
packageJson = await readJson('./package.json');
} catch (err2) {
throw err2;
}
packageJson = DEFUALT_PACKAGE_JSON;
generatedPackageJson = true;
}

const addedDeps = await addDependencies(packageJson, options);
const addedScripts = await addScripts(packageJson, options);
if (addedDeps || addedScripts) {
if (generatedPackageJson || addedDeps || addedScripts) {
await writePackageJson(packageJson, options);
} else {
options.logger.log('No edits needed in package.json.');
Expand Down

0 comments on commit 122fe81

Please sign in to comment.