Skip to content

Commit 9145568

Browse files
committed
chore: Link to paths in package.json directly for ci.
1 parent a0b15ec commit 9145568

File tree

4 files changed

+104
-33
lines changed

4 files changed

+104
-33
lines changed

Diff for: .travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ env:
1111
before_install:
1212
- rm yarn.lock
1313
- npm install -g lerna yarn
14-
- ./scripts/checkout-opticss-and-link.sh
15-
install: lerna bootstrap --registry=https://registry.npmjs.org/
14+
- ./scripts/checkout-opticss.sh build/opticss
15+
- ./scripts/link-to-opticss.js --hard build/opticss
16+
install: lerna bootstrap --registry=https://registry.npmjs.org/

Diff for: scripts/checkout-opticss-and-link.sh

+2-15
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,5 @@ if [[ -z "$OPTICSS_DIR" ]];
55
then
66
OPTICSS_DIR=build/opticss
77
fi
8-
BRANCH="$(node -e "const j = require('./scripts/config.json'); process.stdout.write(j.opticss.branch);")";
9-
mkdir -p $(dirname $OPTICSS_DIR)
10-
if [ -d $OPTICSS_DIR ]
11-
then
12-
cd $OPTICSS_DIR
13-
git fetch origin
14-
git checkout $BRANCH
15-
git pull
16-
cd -
17-
else
18-
git clone -b $BRANCH --depth 1 [email protected]:css-blocks/opticss.git $OPTICSS_DIR
19-
fi
20-
cd $OPTICSS_DIR && rm yarn.lock && lerna bootstrap --registry=https://registry.npmjs.org/
21-
cd -
22-
./scripts/link-to-opticss.js $OPTICSS_DIR
8+
./script/checkout-opticss.sh $OPTICSS_DIR
9+
./scripts/link-to-opticss.js $OPTICSS_DIR

Diff for: scripts/checkout-opticss.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
OPTICSS_DIR=$1
4+
if [[ -z "$OPTICSS_DIR" ]];
5+
then
6+
OPTICSS_DIR=build/opticss
7+
fi
8+
BRANCH="$(node -e "const j = require('./scripts/config.json'); process.stdout.write(j.opticss.branch);")";
9+
mkdir -p $(dirname $OPTICSS_DIR)
10+
if [ -d $OPTICSS_DIR ]
11+
then
12+
cd $OPTICSS_DIR
13+
git fetch origin
14+
git checkout $BRANCH
15+
git pull
16+
cd -
17+
else
18+
git clone -b $BRANCH --depth 1 [email protected]:css-blocks/opticss.git $OPTICSS_DIR
19+
fi
20+
cd $OPTICSS_DIR && rm yarn.lock && lerna bootstrap --registry=https://registry.npmjs.org/
21+
cd -

Diff for: scripts/link-to-opticss.js

+78-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
#!/usr/bin/env node
22
const childProcess = require("child_process");
33
const path = require("path");
4-
const opticssDir = path.resolve(process.argv[2]);
4+
const fs = require("fs")
5+
let args = process.argv.slice(2);
6+
let opts = {
7+
hard: false,
8+
opticssDir: null,
9+
};
10+
function processArg(arg) {
11+
if (arg === "--hard") {
12+
opts.hard = true;
13+
} else if (arg.startsWith("-")) {
14+
throw new Error(`unrecognize option: ${arg}`);
15+
} else if (opts.opticssDir === null) {
16+
opts.opticssDir = path.resolve(arg);
17+
} else {
18+
throw new Error(`unrecognize argument: ${arg}`);
19+
}
20+
}
21+
22+
while (args.length > 0) {
23+
processArg(args.shift());
24+
}
25+
26+
if (!opts.opticssDir) {
27+
throw new Error("No directory for opticss provided.");
28+
}
529

630
function getLernaPackageDirs(monoRepoDir) {
731
let lernaSpec = require(path.join(monoRepoDir, "lerna.json"));
@@ -17,6 +41,7 @@ const blocksDir = path.resolve(__dirname, "..");
1741
const blocksPackageDirs = getLernaPackageDirs(blocksDir);
1842

1943
let depToPackages = {};
44+
let dirToJSON = {}
2045

2146
function recordDependencies(deps, dir) {
2247
if (!deps) return;
@@ -30,26 +55,63 @@ function recordDependencies(deps, dir) {
3055

3156
for (let dir of blocksPackageDirs) {
3257
let pkg = getPackageJson(dir);
58+
dirToJSON[dir] = pkg;
3359
recordDependencies(pkg.dependencies, dir);
3460
recordDependencies(pkg.devDependencies, dir);
3561
}
3662

37-
for (let dir of getLernaPackageDirs(opticssDir)) {
38-
let pkg = getPackageJson(dir);
39-
let name = pkg.name;
40-
let depDirs = depToPackages[name];
41-
if (depDirs) {
42-
try {
43-
console.log(childProcess.execSync(`cd ${dir} && yarn unlink`, {encoding: "utf8"}));
44-
} catch (e) {
45-
//ignore
63+
function symlink() {
64+
for (let dir of getLernaPackageDirs(opts.opticssDir)) {
65+
let pkg = getPackageJson(dir);
66+
let name = pkg.name;
67+
let depDirs = depToPackages[name];
68+
if (depDirs) {
69+
try {
70+
console.log(childProcess.execSync(`cd ${dir} && yarn unlink`, {encoding: "utf8"}));
71+
} catch (e) {
72+
//ignore
73+
}
74+
console.log(childProcess.execSync(`cd ${dir} && yarn link`, {encoding: "utf8"}));
75+
console.log(`linking ${name}@${pkg.version} to shared packages`);
76+
console.log(childProcess.execSync(`cd ${blocksDir} && yarn link ${name}`, {encoding: "utf8"}));
77+
for (let depDir of depDirs) {
78+
console.log(`linking ${name}@${pkg.version} to ${path.relative(path.resolve("."), depDir)}`);
79+
console.log(childProcess.execSync(`cd ${depDir} && yarn link ${name}`, {encoding: "utf8"}));
80+
}
4681
}
47-
console.log(childProcess.execSync(`cd ${dir} && yarn link`, {encoding: "utf8"}));
48-
console.log(`linking ${name}@${pkg.version} to shared packages`);
49-
console.log(childProcess.execSync(`cd ${blocksDir} && yarn link ${name}`, {encoding: "utf8"}));
50-
for (let depDir of depDirs) {
51-
console.log(`linking ${name}@${pkg.version} to ${path.relative(path.resolve("."), depDir)}`);
52-
console.log(childProcess.execSync(`cd ${depDir} && yarn link ${name}`, {encoding: "utf8"}));
82+
}
83+
}
84+
85+
function setDependencyToFile(name, dependencyDir, dependentDir) {
86+
let pkg = dirToJSON[dependentDir];
87+
if (pkg.dependencies[name]) {
88+
pkg.dependencies[name] = `file:${dependencyDir}`;
89+
}
90+
if (pkg.devDependencies[name]) {
91+
pkg.devDependencies[name] = `file:${dependencyDir}`;
92+
}
93+
}
94+
95+
function hardlink() {
96+
for (let depDir of getLernaPackageDirs(opts.opticssDir)) {
97+
let pkg = getPackageJson(depDir);
98+
let name = pkg.name;
99+
let depDirs = depToPackages[name];
100+
if (depDirs) {
101+
for (let dir of depDirs) {
102+
setDependencyToFile(name, depDir, dir)
103+
}
53104
}
54105
}
106+
for (let dir of blocksPackageDirs) {
107+
let pkg = dirToJSON[dir];
108+
let updated = JSON.stringify(pkg, null, 2);
109+
fs.writeFileSync(path.join(dir,"package.json"), updated);
110+
}
111+
}
112+
113+
if (opts.hard) {
114+
hardlink();
115+
} else {
116+
symlink();
55117
}

0 commit comments

Comments
 (0)