Skip to content

Commit

Permalink
Convert more things to TS (#91)
Browse files Browse the repository at this point in the history
* More things to TS

* Add changesets
  • Loading branch information
emmatown authored and Noviny committed Jun 11, 2019
1 parent cc2f006 commit d4bbab4
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .changeset/gentle-knives-explain/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "get-workspaces", "type": "patch" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/gentle-knives-explain/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add PackageJSON type
4 changes: 4 additions & 0 deletions .changeset/unlucky-jeans-juggle/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "@changesets/cli", "type": "patch" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/unlucky-jeans-juggle/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert some internals to TypeScript
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import fs from "fs-extra";
import path from "path";
import semver from "semver";
import { PackageJSON, Workspace } from "get-workspaces";
// @ts-ignore
import * as boltMessages from "bolt/dist/modern/utils/messages";

import { DEPENDENCY_TYPES } from "../constants";

const getAllDependencies = config => {
const getAllDependencies = (config: PackageJSON) => {
const allDependencies = new Map();

for (const type of DEPENDENCY_TYPES) {
Expand All @@ -22,12 +24,18 @@ const getAllDependencies = config => {
return allDependencies;
};

export default async function getDependencyGraph(packages, cwd) {
const graph = new Map();
export default async function getDependencyGraph(
packages: Array<Workspace>,
cwd: string
) {
const graph = new Map<
string,
{ pkg: Workspace; dependencies: Array<string> }
>();
let valid = true;

const pkgRoot = await fs
.readFile(path.resolve(cwd, "package.json"))
.readFile(path.resolve(cwd, "package.json"), "utf8")
.then(JSON.parse);

const pkgRootConfigged = {
Expand All @@ -36,7 +44,9 @@ export default async function getDependencyGraph(packages, cwd) {
dir: path.resolve(cwd)
};

const packagesByName = { [pkgRoot.name]: pkgRootConfigged };
const packagesByName: { [key: string]: Workspace } = {
[pkgRoot.name]: pkgRootConfigged
};

const queue = [pkgRootConfigged];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PackageJSON } from "get-workspaces";
import { DEPENDENCY_TYPES } from "../constants";
import { DependencyType } from "../types";

export function getDependencyTypes(depName, config) {
export function getDependencyTypes(depName: string, config: PackageJSON) {
const matchedTypes = [];
for (const depType of DEPENDENCY_TYPES) {
const deps = getDeps(depType, config);
Expand All @@ -11,7 +13,10 @@ export function getDependencyTypes(depName, config) {
return matchedTypes;
}

export function getDependencyVersionRange(depName, config) {
export function getDependencyVersionRange(
depName: string,
config: PackageJSON
) {
for (const depType of DEPENDENCY_TYPES) {
const deps = getDeps(depType, config);
if (deps && deps[depName]) {
Expand All @@ -21,7 +26,7 @@ export function getDependencyVersionRange(depName, config) {
return null;
}

function getDeps(depType, config) {
function getDeps(depType: DependencyType, config: PackageJSON) {
const deps = config[depType];
if (typeof deps === "undefined") return;
return deps;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import getWorkspaces from "./getWorkspaces";
import getDependencyGraph from "./getDependencyGraph";
import { Workspace } from "get-workspaces";

export default async function getDependentsGraph({ cwd }) {
export default async function getDependentsGraph({ cwd }: { cwd: string }) {
const packages = await getWorkspaces({ cwd });
const graph = new Map();

const { graph: dependencyGraph } = await getDependencyGraph(packages, cwd);

const dependentsLookup = {};
const dependentsLookup: {
[key: string]: { pkg: Workspace; dependents: Array<string> };
} = {};

packages.forEach(pkg => {
dependentsLookup[pkg.config.name] = {
Expand All @@ -18,12 +21,14 @@ export default async function getDependentsGraph({ cwd }) {

packages.forEach(pkg => {
const dependent = pkg.config.name;
const valFromDependencyGraph = dependencyGraph.get(dependent) || {};
const dependencies = valFromDependencyGraph.dependencies || [];

dependencies.forEach(dependency => {
dependentsLookup[dependency].dependents.push(dependent);
});
const valFromDependencyGraph = dependencyGraph.get(dependent);
if (valFromDependencyGraph) {
const dependencies = valFromDependencyGraph.dependencies;

dependencies.forEach(dependency => {
dependentsLookup[dependency].dependents.push(dependent);
});
}
});

// can't use Object.entries here as the flow type for it is Array<[string, mixed]>;
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/utils/bolt-replacements/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @ts-ignore
import getDependentsGraph from "./getDependentsGraph";
import getWorkspaces from "./getWorkspaces";
// @ts-ignore
import publishPackages from "./publishPackages";
// @ts-ignore
import getDependencyGraph from "./getDependencyGraph";

export {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default function versionRangeToRangeType(versionRange) {
export default function versionRangeToRangeType(
versionRange: string
): "^" | "~" | "" {
if (versionRange.charAt(0) === "^") return "^";
if (versionRange.charAt(0) === "~") return "~";
return "";
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ export const DEPENDENCY_TYPES = [
"dependencies",
"devDependencies",
"peerDependencies",
"bundledDependencies",
"optionalDependencies"
];
] as const;
4 changes: 4 additions & 0 deletions packages/cli/src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { DEPENDENCY_TYPES } from "./constants";

export type BumpType = "major" | "minor" | "patch" | "none";

export type DependencyType = typeof DEPENDENCY_TYPES[number];

export type Changeset = {
id: string;
commit: string;
Expand Down
8 changes: 6 additions & 2 deletions packages/get-workspaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ type Options = {
tools?: Array<"yarn" | "bolt" | "root">;
};

type pkgJSON = {
export type PackageJSON = {
name: string;
version: string;
dependencies?: { [key: string]: string };
peerDependencies?: { [key: string]: string };
devDependencies?: { [key: string]: string };
optionalDependencies?: { [key: string]: string };
};

export type Workspace = { config: pkgJSON; name: string; dir: string };
export type Workspace = { config: PackageJSON; name: string; dir: string };

export default async function getWorkspaces(
opts: Options = {}
Expand Down

0 comments on commit d4bbab4

Please sign in to comment.