Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 7a1da89

Browse files
authored
tooling: scripts package cleanup (#2261)
* scripts package cleanup * getAllPackageInfo returns relative paths * really replace git root
1 parent 884067c commit 7a1da89

12 files changed

+118
-47
lines changed

build/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@types/gulp-remember": "^0.0.31",
2525
"@types/jest-axe": "^3.2.1",
2626
"@types/jest": "^24.0.19",
27+
"@types/lerna-alias": "^3.0.0",
2728
"@types/node": "^10.3.2",
2829
"@types/puppeteer": "^1.11.1",
2930
"@types/webpack-dev-middleware": "^2.0.1",

scripts/monorepo/findGitRoot.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs')
2-
const path = require('path')
1+
import fs from 'fs'
2+
import path from 'path'
33

4-
function findGitRoot() {
4+
export function findGitRoot(): string {
55
let cwd = process.cwd()
66
const root = path.parse(cwd).root
77
let found = false
@@ -16,5 +16,3 @@ function findGitRoot() {
1616

1717
return cwd
1818
}
19-
20-
export { findGitRoot }

scripts/monorepo/getAllPackageDeps.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getAllPackageInfo } from './getAllPackageInfo'
1+
import { getAllPackageInfo, PackageJson } from './getAllPackageInfo'
22

3-
function getDeps(packageJson: any, repoPackages: string[]) {
3+
function getDeps(packageJson: PackageJson, repoPackages: string[]) {
44
if (!packageJson) {
55
return []
66
}
@@ -11,9 +11,10 @@ function getDeps(packageJson: any, repoPackages: string[]) {
1111
}
1212

1313
/**
14-
* Find all the dependencies (and their dependencies) within the repo for a specific package (in the CWD when this was called)
14+
* Find all the dependencies (and their dependencies) within the repo for a specific package
15+
* (in the CWD when this was called)
1516
*/
16-
function getAllPackageDeps() {
17+
export function getAllPackageDeps() {
1718
const allInfo = getAllPackageInfo()
1819
const repoPackages = Object.keys(allInfo)
1920
const allDeps = new Map<string, Set<string>>()
@@ -50,4 +51,3 @@ function getAllPackageDeps() {
5051

5152
return allDeps
5253
}
53-
export { getAllPackageDeps }

scripts/monorepo/getAllPackageInfo.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
1-
import { spawnSync } from 'child_process'
1+
import { rollup as lernaAliases } from 'lerna-alias'
22
import path from 'path'
33
import { findGitRoot } from './findGitRoot'
44

5-
interface PackageJson {
5+
export interface PackageJson {
66
name: string
77
version: string
88
main: string
99
types?: string
1010
module?: string
1111
dependencies?: { [key: string]: string }
1212
devDependencies?: { [key: string]: string }
13+
[key: string]: any
1314
}
1415

15-
interface PackageInfo {
16+
export interface PackageInfo {
17+
/** Package path relative to the project root */
1618
packagePath: string
19+
/** package.json contents */
1720
packageJson: PackageJson
1821
}
1922

20-
type AllPackageInfo = { [key: string]: PackageInfo }
23+
export type AllPackageInfo = { [packageName: string]: PackageInfo }
2124

22-
let packageInfoCache: AllPackageInfo = null
25+
let packageInfoCache: AllPackageInfo | null = null
2326

2427
export function getAllPackageInfo(): AllPackageInfo {
2528
if (packageInfoCache) {
2629
return packageInfoCache
2730
}
2831

2932
const gitRoot = findGitRoot()
30-
const results = spawnSync('git', ['ls-tree', '-r', '--name-only', '--full-tree', 'HEAD'])
33+
const packagePaths = lernaAliases({ sourceDirectory: false })
34+
3135
const packageInfo: { [key: string]: PackageInfo } = {}
3236

33-
results.stdout
34-
.toString()
35-
.split('\n')
36-
.map((line: string) => {
37-
return line.trim()
38-
})
39-
.filter((line: string) => line.endsWith('package.json'))
40-
.forEach((packageJsonFile: string) => {
41-
const packageJson = require(path.join(gitRoot, packageJsonFile))
42-
43-
if (packageJson) {
44-
packageInfo[packageJson.name] = {
45-
packagePath: path.dirname(packageJsonFile),
46-
packageJson,
47-
}
37+
for (const [packageName, packagePath] of Object.entries(packagePaths)) {
38+
try {
39+
packageInfo[packageName] = {
40+
packagePath: path.relative(gitRoot, packagePath),
41+
packageJson: require(path.join(packagePath, 'package.json')),
4842
}
49-
})
43+
} catch (ex) {
44+
/* eslint-disable-next-line no-console */
45+
console.warn(`Error parsing package.json for ${packageName}: ${ex}`)
46+
}
47+
}
5048

5149
packageInfoCache = packageInfo
5250

scripts/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"@types/fs-extra": "^8.0.1",
2222
"@types/jest": "^24.0.19",
2323
"@types/jest-environment-puppeteer": "^4.3.1",
24+
"@types/jju": "^1.4.1",
25+
"@types/lerna-alias": "^3.0.0",
2426
"@types/node": "^10.3.2",
2527
"@types/react": "^16.8.10",
2628
"@types/storybook__react": "^4.0.2",
@@ -39,9 +41,12 @@
3941
"http-server": "^0.11.1",
4042
"jest": "^24.9.0",
4143
"jest-puppeteer": "^4.3.0",
44+
"jju": "^1.4.0",
4245
"just-scripts": "^0.35.0",
46+
"lerna-alias": "^3.0.3-0",
4347
"puppeteer": "^1.13.0",
4448
"react-docgen-typescript-loader": "^3.2.0",
49+
"read-package-json": "^2.0.13",
4550
"rollup": "^1.25.1",
4651
"rollup-plugin-dts": "^1.1.10",
4752
"ts-jest": "^24.1.0",

scripts/tasks/autoProjectRefsTask.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import { getAllPackageDeps } from '../monorepo/getAllPackageDeps'
22
import path from 'path'
33
import fs from 'fs'
4+
import jju from 'jju'
45
import { getAllPackageInfo } from '../monorepo/getAllPackageInfo'
56
import { findGitRoot } from '../monorepo/findGitRoot'
67
import { logger } from 'just-task'
78

9+
interface TSConfig {
10+
references: { path: string }[]
11+
[key: string]: any
12+
}
13+
814
export function autoProjectRefsTask() {
915
const updatedTsconfigs = getUpdatedTsconfigs()
1016

1117
if (updatedTsconfigs.size > 0) {
12-
for (const [file, blob] of updatedTsconfigs.entries()) {
18+
for (const [file, [originalText, updatedJson]] of updatedTsconfigs.entries()) {
1319
logger.info(`Updating ${file} with project references`)
14-
fs.writeFileSync(file, JSON.stringify(blob, null, 2))
20+
fs.writeFileSync(
21+
file,
22+
jju.update(originalText, updatedJson, {
23+
mode: 'json5',
24+
indent: 2,
25+
quote: '"',
26+
quote_keys: true,
27+
no_trailing_comma: true,
28+
}),
29+
)
1530
}
1631
}
1732
}
@@ -28,22 +43,23 @@ export function autoProjectRefsVerifyTask() {
2843
}
2944

3045
function getUpdatedTsconfigs() {
31-
const excluded = ['@fluentui/scripts', 'fluent-ui-monorepo']
46+
const excluded = ['@fluentui/scripts', '@fluentui/build']
3247
const repoDeps = getAllPackageDeps()
3348
const allInfo = getAllPackageInfo()
3449
const root = findGitRoot()
3550
let isDirty = false
36-
const updatedTsconfigs = new Map<string, string>()
51+
const updatedTsconfigs = new Map<string, [string, TSConfig]>()
3752

3853
for (const [name, info] of Object.entries(allInfo)) {
3954
if (excluded.includes(name)) {
4055
continue
4156
}
4257

4358
isDirty = false
44-
const deps = repoDeps.get(name)
59+
const deps = repoDeps.get(name)!
4560
const tsconfigFile = path.join(root, info.packagePath, 'tsconfig.json')
46-
const tsconfigJson = JSON.parse(fs.readFileSync(tsconfigFile, 'utf-8'))
61+
const tsconfigText = fs.readFileSync(tsconfigFile, 'utf-8')
62+
const tsconfigJson: TSConfig = jju.parse(tsconfigText)
4763

4864
const newRefs = [...deps]
4965
.sort()
@@ -64,7 +80,7 @@ function getUpdatedTsconfigs() {
6480
}
6581

6682
if (isDirty) {
67-
updatedTsconfigs.set(tsconfigFile, tsconfigJson)
83+
updatedTsconfigs.set(tsconfigFile, [tsconfigText, tsconfigJson])
6884
}
6985
}
7086
return updatedTsconfigs

scripts/tasks/e2eTask.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import { jestTask, resolveCwd } from 'just-scripts'
22
import path from 'path'
33
import os from 'os'
44

5+
const noOp = (done: () => void) => done()
6+
57
export function e2eTask() {
8+
const config = resolveCwd('jest.puppeteer.js')
9+
if (!config) {
10+
return noOp
11+
}
12+
613
return jestTask({
714
runInBand: true,
8-
config: resolveCwd('jest.puppeteer.js'),
15+
config,
916
env: {
1017
...process.env,
1118
JEST_E2E_HEADLESS: 'true',
@@ -16,9 +23,14 @@ export function e2eTask() {
1623
}
1724

1825
export function e2eWatchTask() {
26+
const config = resolveCwd('jest.puppeteer.js')
27+
if (!config) {
28+
return noOp
29+
}
30+
1931
return jestTask({
2032
runInBand: true,
21-
config: resolveCwd('jest.puppeteer.js'),
33+
config,
2234
_: ['--watchAll'],
2335
env: {
2436
...process.env,
@@ -28,12 +40,17 @@ export function e2eWatchTask() {
2840
}
2941

3042
export function e2ePerfTask() {
43+
const config = resolveCwd('jest.puppeteer.js')
44+
if (!config) {
45+
return noOp
46+
}
47+
3148
const tmpDir = os.tmpdir()
3249
const logFile = path.join(tmpDir, 'puppeteer.log')
3350

3451
return jestTask({
3552
runInBand: true,
36-
config: resolveCwd('jest.puppeteer.js'),
53+
config,
3754
env: {
3855
...process.env,
3956
JEST_E2E_PROFILE: logFile,

scripts/tasks/httpServerTask.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { createServer, Options } from 'http-server'
22
import { ListenOptions } from 'net'
33

44
export function httpServerTask(options: Options & ListenOptions = {}) {
5-
return function httpServer(done) {
5+
return function httpServer(done: () => void) {
66
const server = createServer(options)
77
server.listen({ host: 'localhost', port: options.port }, () => {
8+
/* eslint-disable-next-line no-console */
89
console.log(`running on http://localhost:${options.port}/`)
910
done()
1011
})

scripts/tasks/publishPrepareTask.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,37 @@ import { findGitRoot } from '../monorepo/findGitRoot'
33
import fs from 'fs'
44
import path from 'path'
55

6-
export function publishPrepareTask() {
6+
export interface IPublishPrepareOptions {
7+
/** Don't update package.json for these packages */
8+
excludePackages?: string[]
9+
}
10+
11+
/**
12+
* Update package.json for each project so that its `main` points to the build output
13+
* rather than the source.
14+
*/
15+
export function publishPrepare(options: IPublishPrepareOptions = {}) {
716
const root = findGitRoot()
817
const allInfo = getAllPackageInfo()
918

10-
for (const info of Object.values(allInfo)) {
11-
if (info.packageJson.main && info.packageJson.main.startsWith('src/index')) {
19+
const { excludePackages = [] } = options
20+
21+
for (const [packageName, info] of Object.entries(allInfo)) {
22+
const oldMain = info.packageJson.main
23+
if (oldMain && oldMain.startsWith('src/') && !excludePackages.includes(packageName)) {
1224
const newPackageJson = {
1325
...info.packageJson,
14-
main: info.packageJson.main.replace('src/', 'lib/').replace(/\.tsx?/, '.js'),
26+
main: oldMain.replace('src/', 'lib/').replace(/\.tsx?/, '.js'),
1527
}
28+
1629
fs.writeFileSync(
1730
path.join(root, info.packagePath, 'package.json'),
1831
`${JSON.stringify(newPackageJson, null, 2)}\n`,
1932
)
2033
}
2134
}
2235
}
36+
37+
export function publishPrepareTask() {
38+
publishPrepare()
39+
}

scripts/tasks/storybookTask.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { resolveCwd, argv } from 'just-scripts'
22
import path from 'path'
33

4+
// @ts-ignore
45
import storybook from '@storybook/react/standalone'
56

67
interface StorybookTaskOptions {

scripts/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./config/typescript/tsconfig.common.json",
3+
"compilerOptions": {
4+
"target": "es2015",
5+
"lib": ["es2015"]
6+
}
7+
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4348,6 +4348,11 @@
43484348
dependencies:
43494349
jest-diff "^24.3.0"
43504350

4351+
"@types/jju@^1.4.1":
4352+
version "1.4.1"
4353+
resolved "https://registry.yarnpkg.com/@types/jju/-/jju-1.4.1.tgz#0a39f5f8e84fec46150a7b9ca985c3f89ad98e9f"
4354+
integrity sha512-LFt+YA7Lv2IZROMwokZKiPNORAV5N3huMs3IKnzlE430HWhWYZ8b+78HiwJXJJP1V2IEjinyJURuRJfGoaFSIA==
4355+
43514356
"@types/json-schema@^7.0.3":
43524357
version "7.0.3"
43534358
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
@@ -4366,6 +4371,11 @@
43664371
csstype "^2.0.0"
43674372
indefinite-observable "^1.0.1"
43684373

4374+
"@types/lerna-alias@^3.0.0":
4375+
version "3.0.0"
4376+
resolved "https://registry.yarnpkg.com/@types/lerna-alias/-/lerna-alias-3.0.0.tgz#e4d70f75bad9f5a7b697f76b7d7b3fa97aa57844"
4377+
integrity sha512-EfwEzSWxAxrZgUJT5sECi4RPFCIgOm/K9e5EWXp2hR9H6wzV1QIBgjGtGEl/OpOCcglNWzmqrVRX/qjvNwli9Q==
4378+
43694379
"@types/lodash@^4.14.118":
43704380
version "4.14.118"
43714381
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.118.tgz#247bab39bfcc6d910d4927c6e06cbc70ec376f27"

0 commit comments

Comments
 (0)