Skip to content

Commit

Permalink
start working with custom lerna command
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance Strickland committed Jul 24, 2020
1 parent 85e5400 commit f0795f7
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ max_line_length = 80
tab_width = 2
trim_trailing_whitespace = true

[Makefile]
indent_style = tab
tab_width = 4

[{*.md,*.mdx}]
trim_trailing_whitespace = false
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
YARN := yarn --silent
NODE := $(YARN) node

build:
$(YARN) lerna run build --stream --ignore @interop-ui/docs

clean:
rm -rf packages/*/*/dist
rm -rf packages/*/*/npm-debug*

clean-all:
rm -rf node_modules
rm -rf yarn.lock
rm -rf .cache
$(MAKE) clean

lerna-bootstrap: clean-all
$(YARN) --ignore-engines

lerna-bootstrap: clean-all
$(YARN) --ignore-engines
$(YARN) lerna bootstrap -- -- --ignore-engines

bootstrap: lerna-bootstrap
$(MAKE) build
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lerna": "3.0.0",
"lerna": "1.0.0",
"useWorkspaces": true,
"version": "independent",
"npmClient": "yarn"
Expand Down
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"scripts": {
"lint": "eslint --ext .js,.ts,.tsx .",
"test": "cross-env CI=true ts-node --transpile-only ./scripts/test packages",
"build": "lerna run build --stream --ignore @interop-ui/docs",
"clean": "lerna run clean --stream",
"bootstrap": "make bootstrap",
"// build": "make build",
"build": "ts-node --transpile-only scripts/build",
"clean": "make clean",
"new-component": "cross-env CI=true ts-node ./scripts/new-component",
"nc": "yarn new-component",
"storybook": "start-storybook -p 9009 -c ./storybook --ci",
Expand Down Expand Up @@ -56,6 +58,13 @@
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
"@babel/plugin-proposal-optional-chaining": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@lerna/command": "^3.21.0",
"@lerna/output": "^3.13.0",
"@lerna/profiler": "^3.20.0",
"@lerna/timer": "^3.13.0",
"@lerna/validation-error": "^3.13.0",
"@lerna/filter-options": "^3.20.0",
"@lerna/run-topologically": "^3.18.5",
"@rollup/plugin-babel": "^5.1.0",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-json": "^4.1.0",
Expand Down
100 changes: 94 additions & 6 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { babel } from '@rollup/plugin-babel';
import sourceMaps from 'rollup-plugin-sourcemaps';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import { CompilerOptions } from 'typescript';
import { paths } from './constants';
import {
external,
Expand All @@ -23,12 +24,40 @@ import {
} from './utils';
import { ScriptOpts, NormalizedOpts } from './types';
import babelConfig from './config/babel';
import { execSync, spawn, spawnSync } from 'child_process';

let rootTsConfig: TSConfigJSON = fs.readJSONSync(path.join(paths.projectRoot, 'tsconfig.json'));

// `lerna run` will set off a new node process for each package, which is pretty dang slow. Instead
// we can use leverage the --toposort flag to get a list of all packages in the order we want them
// built and just run the script once.
let std = execSync(`yarn lerna ls --toposort`, {
// stdio: 'inherit'
});

let topoPackageMap: Record<string, string> = {};
try {
topoPackageMap = std
.toString()
.split('\n')
.filter((line) => line.startsWith('@interop-ui'))
.reduce((acc, cur) => {
if (rootTsConfig.compilerOptions.paths![cur]) {
return {
...acc,
[cur]: rootTsConfig.compilerOptions.paths![cur][0],
};
}
return acc;
}, {});
} catch (err) {
console.warn('Something went wrong when looking for packages to build.');
process.exit(1);
}

// https://github.com/formium/tsdx/blob/3c65bdf90860c45a619b8a23720e41f8e251a4ef/src/createRollupConfig.ts#L24
let shebang: any = {};

let rootTsConfig = fs.readJSONSync(path.join(paths.projectRoot, 'tsconfig.json'));

export async function createRollupConfig(
opts: ScriptOpts,
buildCount: number
Expand Down Expand Up @@ -73,7 +102,7 @@ export async function createRollupConfig(
// namespaceImportObject from...) that are accessed dynamically.
freeze: false,
// Respect tsconfig esModuleInterop when setting __esModule.
esModule: (tsconfigJSON && tsconfigJSON.esModuleInterop) || false,
esModule: (tsconfigJSON && tsconfigJSON.compilerOptions.esModuleInterop) || false,
},
plugins: [
nodeResolve({ mainFields }),
Expand Down Expand Up @@ -158,8 +187,8 @@ export async function createRollupConfig(
};
}

async function build() {
const opts = await normalizeOpts(parseArgs());
async function build(opts: NormalizedOpts) {
//const opts = await normalizeOpts(parseArgs());
const buildConfigs = await createBuildConfigs(opts);

try {
Expand Down Expand Up @@ -190,7 +219,31 @@ async function build() {
}
}

build();
async function buildAll() {
let execs: (() => any)[] = [];
for (let packageName in topoPackageMap) {
let inputDir = path.join(paths.projectRoot, topoPackageMap[packageName]);
let input = [
fs.existsSync(path.join(inputDir, 'index.ts'))
? path.join(inputDir, 'index.ts')
: path.join(inputDir, 'index.tss'),
];
let name = packageName.split('/')[1];
console.log({ input, name });
execs.push(() => build({ name, input }));
}
serial(...execs);
}

function serial<T extends ((...args: any[]) => any)[]>(...funcs: T) {
return funcs.reduce(
(promise: Promise<any>, func) =>
promise.then((result) => func().then(Array.prototype.concat.bind(result))),
Promise.resolve([])
);
}

buildAll();

export function writeCjsEntryFile(name: string) {
const contents = `'use strict';
Expand Down Expand Up @@ -265,3 +318,38 @@ async function moveDeclarationFilesToDistTypes(packageName: string) {
await fs.remove(path.resolve(paths.packageDistTypes, relativeDirToDelete));
} catch (e) {}
}

type TSConfigJSON = {
/**
* If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to
* including all files in the containing directory and subdirectories except those specified by
* 'exclude'. When a 'files' property is specified, only those files and those specified by
* 'include' are included.,
*/
files?: string[];
/**
* Specifies a list of files to be excluded from compilation. The 'exclude' property only affects
* the files included via the 'include' property and not the 'files' property. Glob patterns
* require TypeScript version 2.0 or later.
*/
exclude?: string[];
/**
* Specifies a list of glob patterns that match files to be included in compilation. If no
* 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to
* including all files in the containing directory and subdirectories except those specified by
* 'exclude'. Requires TypeScript version 2.0 or later.
*/
include?: string[];
/**
* Enable Compile-on-Save for this project.
*/
compileOnSave?: boolean;
/**
* Path to base configuration file to inherit from. Requires TypeScript version 2.1 or later.
*/
extends?: string;
/**
* Instructs the TypeScript compiler how to compile .ts files.
*/
compilerOptions: CompilerOptions;
};
Loading

0 comments on commit f0795f7

Please sign in to comment.