Skip to content

Commit

Permalink
Add static types (Flow)
Browse files Browse the repository at this point in the history
  • Loading branch information
guigrpa committed Feb 16, 2017
1 parent 2b0b264 commit 90d0a1b
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yaml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
parser: babel-eslint
extends: airbnb
plugins:
- flowtype
rules:
eqeqeq: ['error', 'allow-null']
no-unused-expressions:
Expand Down
6 changes: 6 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[ignore]
<PROJECT_ROOT>.*/__tests__/.*
<PROJECT_ROOT>.*/__mocks__/.*
<PROJECT_ROOT>/lib/.*
<PROJECT_ROOT>/node_modules/fbjs
<PROJECT_ROOT>/node_modules/jest/.*
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

* Add **tentative support for scoped packages**.
* Internal:
- Add unit tests
- Add static types (Flow)

## 0.5.1 (Feb. 15, 2017)

* Add **`oao upgrade <sub-package> [deps...]`**
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"oao": "lib/index.js"
},
"scripts": {
"build": "yarn lint && yarn compile && yarn test && yarn xxl",
"build": "yarn lint && yarn flow && yarn compile && yarn test && yarn xxl",
"travis": "yarn test",
"lint": "eslint src",
"flow": "flow check || exit 0",
"compile": "rm -rf lib && babel src -d lib --ignore \"**/__mocks__/**\",\"**/__tests__/**\"",
"jest": "jest --watch --coverage --verbose",
"test": "yarn testCovFull",
Expand Down Expand Up @@ -63,6 +64,7 @@
"eslint-plugin-import": "2.2.0",
"eslint-plugin-jsx-a11y": "4.0.0",
"eslint-plugin-react": "6.9.0",
"flow-bin": "0.39.0",
"jest": "18.1.0",
"nyc": "10.1.2",
"xxl": "^1.0.1"
Expand Down
19 changes: 16 additions & 3 deletions src/addRemoveUpgrade.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @flow

import { merge, set as timmSet } from 'timm';
import { mainStory, chalk } from 'storyboard';
import { mainStory } from 'storyboard';
import kebabCase from 'kebab-case';
import { readAllSpecs, readOneSpec } from './utils/readSpecs';
import removeInternalLinks from './utils/removeInternalLinks';
Expand All @@ -8,7 +10,18 @@ import { exec } from './utils/shell';

const PASS_THROUGH_OPTS = ['dev', 'peer', 'optional', 'exact', 'tilde', 'ignoreEngines'];

const run = async (pkgName, op, deps, opts) => {
type Operation = 'add' | 'remove' | 'upgrade';
type Options = {
src: string,
dev?: boolean,
peer?: boolean,
optional?: boolean,
exact?: boolean,
tilde?: boolean,
ignoreEngines?: boolean,
};

const run = async (pkgName: string, op: Operation, deps: Array<string>, opts: Options) => {
const { src: srcPatterns } = opts;
const allSpecs = await readAllSpecs(srcPatterns);
if (!allSpecs[pkgName]) {
Expand All @@ -22,7 +35,7 @@ const run = async (pkgName, op, deps, opts) => {
const { nextSpecs, removedPackagesByType } = removeInternalLinks(prevSpecs, pkgNames);
try {
if (nextSpecs !== prevSpecs) writeSpecs(specPath, nextSpecs);
mainStory.info(`Installing ${chalk.yellow.bold(deps.join(', '))}...`);
mainStory.info(`Executing 'yarn ${op}'...`);
let cmd = `yarn ${op}`;
if (deps.length) cmd += ` ${deps.join(' ')}`;
PASS_THROUGH_OPTS.forEach((key) => { if (opts[key]) cmd += ` --${kebabCase(key)}`; });
Expand Down
6 changes: 5 additions & 1 deletion src/all.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// @flow

import listPaths from './utils/listPaths';
import { exec } from './utils/shell';

const run = async (cmd, { src: srcPatterns }) => {
type Options = {| src: string |};

const run = async (cmd: string, { src: srcPatterns }: Options) => {
const pkgPaths = await listPaths(srcPatterns);
for (let i = 0; i < pkgPaths.length; i += 1) {
await exec(cmd, { cwd: pkgPaths[i] });
Expand Down
6 changes: 5 additions & 1 deletion src/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

import { mainStory, chalk } from 'storyboard';
import { readAllSpecs, ROOT_PACKAGE } from './utils/readSpecs';
import removeInternalLinks from './utils/removeInternalLinks';
Expand All @@ -6,7 +8,9 @@ import { exec } from './utils/shell';

const DEP_TYPES = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];

const run = async ({ src: srcPatterns }) => {
type Options = {| src: string |};

const run = async ({ src: srcPatterns }: Options) => {
const allSpecs = await readAllSpecs(srcPatterns);
const pkgNames = Object.keys(allSpecs);

Expand Down
5 changes: 4 additions & 1 deletion src/prepublish.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

import path from 'path';
import { merge } from 'timm';
import semver from 'semver';
Expand All @@ -11,8 +13,9 @@ const COPY_SPECS = [
'author', 'license',
'homepage', 'bugs', 'repository',
];
type Options = {| src: string |};

const run = async ({ src: srcPatterns }) => {
const run = async ({ src: srcPatterns }: Options) => {
const allSpecs = await readAllSpecs(srcPatterns);
const pkgNames = Object.keys(allSpecs);
const rootSpecs = allSpecs[ROOT_PACKAGE].specs;
Expand Down
14 changes: 13 additions & 1 deletion src/publish.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

import semver from 'semver';
import inquirer from 'inquirer';
import { mainStory, chalk } from 'storyboard';
Expand All @@ -17,12 +19,22 @@ import {

const DEBUG_SKIP_CHECKS = false;

type Options = {|
src: string,
master: boolean,
confirm: boolean,
publishTag?: string,
_autoVersion?: boolean,
|};

const run = async ({
src: srcPatterns,
master,
confirm,
publishTag,
}) => {
// For unit tests
_autoVersion = false,
}: Options) => {
const allSpecs = await readAllSpecs(srcPatterns);

// Confirm that we have run build
Expand Down
9 changes: 8 additions & 1 deletion src/resetAllVersions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// @flow

import { set as timmSet } from 'timm';
import { mainStory, chalk } from 'storyboard';
import inquirer from 'inquirer';
import semver from 'semver';
import { readAllSpecs } from './utils/readSpecs';
import writeSpecs from './utils/writeSpecs';

const run = async (version, { src: srcPatterns, confirm = true }) => {
type Options = {|
src: string,
confirm?: boolean,
|};

const run = async (version: string, { src: srcPatterns, confirm = true }: Options) => {
if (!semver.valid(version)) {
mainStory.error(`Version ${version} is not valid`);
throw new Error('INVALID_VERSION');
Expand Down
19 changes: 11 additions & 8 deletions src/utils/git.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow

import { exec } from './shell';

const gitLastTag = async () => {
const gitLastTag = async (): Promise<?string> => {
try {
let { stdout: commit } = await exec('git rev-list --tags --max-count=1', {
logLevel: 'trace',
Expand All @@ -17,37 +19,38 @@ const gitLastTag = async () => {
}
};

const gitCurBranch = async () => {
const gitCurBranch = async (): Promise<string> => {
const { stdout } = await exec('git symbolic-ref --short HEAD', { logLevel: 'trace' });
return stdout.trim();
};

const gitUncommittedChanges = async () => {
const gitUncommittedChanges = async (): Promise<string> => {
const { stdout } = await exec('git status --porcelain', { logLevel: 'trace' });
return stdout.trim();
};

// Ripped off from: https://github.com/sindresorhus/np/blob/master/lib/git.js
const gitUnpulledChanges = async () => {
const gitUnpulledChanges = async (): Promise<string> => {
const { stdout } = await exec('git rev-list --count --left-only @{u}...HEAD', { logLevel: 'trace' });
return stdout.trim();
};

const gitDiffSinceIn = async (sinceTag, inPath) => {
const gitDiffSinceIn = async (sinceTag: ?string, inPath: string): Promise<string> => {
if (sinceTag == null) return 'CHANGED';
const { stdout } = await exec(`git diff --name-only ${sinceTag} -- ${inPath}`, { logLevel: 'trace' });
return stdout.trim();
};

const gitCommitChanges = async (msg) => {
const gitCommitChanges = async (msg: string): Promise<void> => {
await exec('git add .', { logLevel: 'trace' });
await exec(`git commit -m ${msg}`, { logLevel: 'trace' });
};

const gitAddTag = async (tag) => {
const gitAddTag = async (tag: string): Promise<void> => {
await exec(`git tag ${tag}`, { logLevel: 'trace' });
};

const gitPushWithTags = async () => {
const gitPushWithTags = async (): Promise<void> => {
await exec('git push --quiet', { logLevel: 'trace' });
await exec('git push --tags --quiet', { logLevel: 'trace' });
};
Expand Down
4 changes: 3 additions & 1 deletion src/utils/listPaths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @flow

import globby from 'globby';

const listPaths = (srcPatterns) => globby(srcPatterns);
const listPaths = (srcPatterns: string): Promise<Array<string>> => globby(srcPatterns);

export default listPaths;
13 changes: 12 additions & 1 deletion src/utils/readSpecs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// @flow

import path from 'path';
import fs from 'fs';
import { mainStory } from 'storyboard';
import listPaths from './listPaths';

const ROOT_PACKAGE = '__ROOT_PACKAGE__';

const readAllSpecs = async (srcPatterns) => {
type PackageName = string;
type OaoSpecs = {
pkgPath: string,
specPath: string, // including .package.json
name: string,
specs: Object,
};
type AllSpecs = { [key: PackageName]: OaoSpecs };

const readAllSpecs = async (srcPatterns: string): Promise<AllSpecs> => {
const pkgPaths = await listPaths(srcPatterns);
pkgPaths.push('.');
const allSpecs = {};
Expand Down
10 changes: 9 additions & 1 deletion src/utils/removeInternalLinks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// @flow

import { omit, set as timmSet } from 'timm';

const DEP_TYPES = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];

const removeInternalLinks = (prevSpecs, pkgNames) => {
type PkgVersionMap = { [pkgName: string]: string };

const removeInternalLinks = (prevSpecs: Object, pkgNames: Array<string>): {
nextSpecs: Object,
removedPackagesByType: { [key: string]: PkgVersionMap },
allRemovedPackages: PkgVersionMap,
} => {
const removedPackagesByType = {};
const allRemovedPackages = {};

Expand Down
26 changes: 21 additions & 5 deletions src/utils/shell.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
// @flow

/* eslint-disable no-underscore-dangle */

import path from 'path';
import shell from 'shelljs';
import split from 'split';
import { mainStory, chalk } from 'storyboard';
import type { StoryT } from 'storyboard';

const cd = (dir, { story = mainStory } = {}) => {
const cd = (dir: string, { story = mainStory }: { story?: StoryT } = {}) => {
story.trace(`Changing working directory to ${chalk.cyan.bold(dir)}...`);
shell.cd(dir);
};

const cp = (src, dst, { story = mainStory } = {}) => {
const cp = (src: string, dst: string, { story = mainStory }: { story?: StoryT } = {}) => {
story.debug(`Copying ${chalk.cyan.bold(src)} -> ${chalk.cyan.bold(dst)}...`);
shell.cp('-rf', path.normalize(src), path.normalize(dst));
};

const mv = (src, dst, { story = mainStory } = {}) => {
const mv = (src: string, dst: string, { story = mainStory }: { story?: StoryT } = {}) => {
story.debug(`Moving ${chalk.cyan.bold(src)} -> ${chalk.cyan.bold(dst)}...`);
shell.mv('-rf', path.normalize(src), path.normalize(dst));
};

const exec = async (cmd, {
type ExecOptions = {|
story?: StoryT,
logLevel?: *,
errorLogLevel?: string,
cwd?: string,
|};

type ExecResult = {
code: number,
stdout: string,
stderr: string,
};

const exec = async (cmd: string, {
story = mainStory,
logLevel = 'info',
errorLogLevel = 'error',
cwd,
} = {}) => {
}: ExecOptions = {}): Promise<ExecResult> => {
const prevWd = shell.pwd();
let title = `Run cmd ${chalk.green.bold(cmd)}`;
if (cwd) title += ` at ${chalk.green(cwd)}`;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/writeSpecs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow

import fs from 'fs';

const writeSpecs = (specPath, specs) => {
const writeSpecs = (specPath: string, specs: Object) => {
fs.writeFileSync(specPath, `${JSON.stringify(specs, null, 2)}\n`, 'utf8');
};

Expand Down
8 changes: 6 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,10 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"

[email protected]:
version "0.39.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.39.0.tgz#b1012a14460df1aa79d3a728e10f93c6944226d0"

for-in@^0.1.5:
version "0.1.6"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-0.1.6.tgz#c9f96e89bfad18a545af5ec3ed352a1d9e5b4dc8"
Expand Down Expand Up @@ -2372,14 +2376,14 @@ js-tokens@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"

[email protected]:
[email protected], js-yaml@^3.5.1:
version "3.6.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
dependencies:
argparse "^1.0.7"
esprima "^2.6.0"

js-yaml@^3.5.1, js-yaml@^3.7.0:
js-yaml@^3.7.0:
version "3.8.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.1.tgz#782ba50200be7b9e5a8537001b7804db3ad02628"
dependencies:
Expand Down

0 comments on commit 90d0a1b

Please sign in to comment.