Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(init): init package with better main, types #179

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/init.ts
Original file line number Diff line number Diff line change
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did the type annotation here disappear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wasn't an annotation but rather a cast. The cast was not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also – we don't use this yet – but going forward we would stronger types for package.json available from new resolveJsonModule in TS 2.9.


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