2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-04 18:23:17 +00:00

Reorganize monorepo helpers, support multiple actions

This commit is contained in:
Vjacheslav Trushkin 2022-01-28 11:24:29 +02:00
parent c464934771
commit d836e30ab9
22 changed files with 143 additions and 107 deletions

View File

@ -74,14 +74,16 @@ This monorepo used Lerna to manage packages, but due to few bugs in Lerna, it wa
To install dependencies in all packages, run
```bash
npm run bootstrap
npm install
```
This will install all dependencies and create symbolic links to packages.
If links stop working for some reason, run `npm run link` to fix links.
If you want to re-install dependencies, run `npm run clean` to clear all repositories (press "Y" to continue), then `npm run bootstrap` to install everything again.
If you want to remove `node_modules` for all packages, run `npm run clean`.
If you want to re-install dependencies, run `npm run reinstall`.
## Documentation

13
monorepo/bootstrap.js vendored
View File

@ -1,13 +0,0 @@
/**
* This script installs all dependencies and creates symbolic links for all packages inside monorepo.
*/
const { build } = require('./build');
build()
.then((functions) => {
functions.installAllPackages();
functions.fixLinks();
})
.catch((err) => {
process.exit(err);
});

View File

@ -1,9 +0,0 @@
const { build } = require('./build');
build()
.then((functions) => {
functions.cleanWorkspaces();
})
.catch((err) => {
process.exit(err);
});

View File

@ -74,6 +74,17 @@ function build() {
});
}
module.exports = {
build,
};
/**
* Export or build
*/
if (!module.parent) {
build()
.then((functions) => {
functions.run();
})
.catch((err) => {
console.error(err);
});
} else {
module.exports = { build };
}

View File

@ -1,14 +0,0 @@
/**
* This script creates symbolic links for all packages inside monorepo.
*
* `lerna link --force-local` does not link dependencies of dependencies, but this fix does.
*/
const { build } = require('./build');
build()
.then((functions) => {
functions.fixLinks();
})
.catch((err) => {
process.exit(err);
});

View File

@ -3,6 +3,7 @@
"private": true,
"version": "0.0.1",
"license": "MIT",
"main": "index.js",
"scripts": {
"build": "tsc -b"
},

View File

@ -0,0 +1,17 @@
import { PackageInfo } from './types';
import { findWorkspaces } from './workspaces';
/**
* Run callback for all workspaces
*/
export function runAction(
log: string,
callback: (workspace: PackageInfo) => void
): void {
const workspaces = findWorkspaces();
if (!workspaces.length) {
throw new Error('No packages found');
}
console.log(`${log}...`);
workspaces.forEach(callback);
}

View File

@ -2,7 +2,7 @@ import fs from 'fs';
import { dirname } from 'path';
import type { PathList } from './types';
export const fixDir = dirname(__dirname);
export const fixDir = dirname(dirname(__dirname));
export const rootDir = dirname(fixDir);
/**

View File

@ -7,7 +7,7 @@ import { PackageInfo } from './types';
*/
export function runNPMCommand(workspace: PackageInfo, params: string[]): void {
const cwd = pathToString(workspace.path);
console.log(relativePath(cwd) + ':', 'npm', params.join(' '));
console.log('\n' + relativePath(cwd) + ':', 'npm', params.join(' '));
const result = spawnSync('npm', params, {
cwd,
stdio: 'inherit',

View File

@ -63,7 +63,7 @@ function rmdir(dir: string) {
try {
const stat = fs.lstatSync(dir);
if (stat.isDirectory() || stat.isSymbolicLink()) {
console.log('Removing', dir);
console.log('Removing', relativePath(dir));
fs.rmSync(dir, {
recursive: true,
});

View File

@ -1,34 +1,94 @@
import { addLinksToWorkspace } from './add-links';
import { cleanWorkspace } from './clean';
import { installAllPackages } from './install';
import { removeLinksFromWorkspace } from './remove-links';
import { findWorkspaces } from './workspaces';
import { runAction } from './helpers/action';
import { addLinksToWorkspace } from './helpers/add-links';
import { cleanWorkspace } from './helpers/clean';
import { runNPMCommand } from './helpers/exec';
import { removeLinksFromWorkspace } from './helpers/remove-links';
/**
* Fix links
* All actions
*/
export function fixLinks(): void {
const workspaces = findWorkspaces();
workspaces.forEach(addLinksToWorkspace);
const actionFunctions: Record<string, () => void> = {
link: () => {
runAction('Creating/fixing symbolic links', addLinksToWorkspace);
},
unlink: () => {
runAction('Removing symbolic links', removeLinksFromWorkspace);
},
clean: () => {
runAction('Removing node_modules', cleanWorkspace);
},
install: () => {
runAction('Installing dependencies', (workspace) => {
runNPMCommand(workspace, ['install']);
});
},
};
/**
* Actions that require parameter
*/
interface ActionWithParam {
action: string;
param: string;
}
const actionWithParamsFunctions: Record<string, (param: string) => void> = {
run: (param: string) => {
runAction(`Running "npm run ${param}"`, (workspace) => {
runNPMCommand(workspace, ['run', param]);
});
},
};
/**
* Remove links
* Run code
*/
export function removeLinks(): void {
const workspaces = findWorkspaces();
workspaces.forEach(removeLinksFromWorkspace);
}
/**
* Install all packages
*/
export { installAllPackages };
/**
* Clean
*/
export function cleanWorkspaces(): void {
const workspaces = findWorkspaces();
workspaces.forEach(cleanWorkspace);
export function run() {
// List of actions
const actions: (string | ActionWithParam)[] = [];
// Process args
let nextActionParam: string | null = null;
process.argv.slice(2).forEach((arg) => {
// Parameter for action with param
if (nextActionParam !== null) {
actions.push({
action: nextActionParam,
param: arg,
});
nextActionParam = null;
return;
}
// Action
if (actionFunctions[arg] !== void 0) {
actions.push(arg);
return;
}
// Action with parameter
if (actionWithParamsFunctions[arg] !== void 0) {
nextActionParam = arg;
return;
}
// Invalid argument
throw new Error(`Invalid argument: ${arg}`);
});
// Make sure arguments list is complete
if (nextActionParam !== null) {
throw new Error(`Missing parameter for action: ${nextActionParam}`);
}
// Run actions
if (!actions.length) {
throw new Error('Nothing to do');
}
actions.forEach((action) => {
if (typeof action === 'string') {
actionFunctions[action]();
} else {
actionWithParamsFunctions[action.action](action.param);
}
});
}

View File

@ -1,20 +0,0 @@
import { runNPMCommand } from './exec';
import { PackageInfo } from './types';
import { findWorkspaces } from './workspaces';
/**
* Install packages
*/
export function installAllPackages(): void {
const workspaces = findWorkspaces();
for (let i = 0; i < workspaces.length; i++) {
installPackages(workspaces[i]);
}
}
/**
* Install packages in a workspace
*/
export function installPackages(workspace: PackageInfo): void {
runNPMCommand(workspace, ['install']);
}

View File

@ -1,12 +0,0 @@
/**
* This script removes symbolic links for all packages inside monorepo.
*/
const { build } = require('./build');
build()
.then((functions) => {
functions.removeLinks();
})
.catch((err) => {
process.exit(err);
});

13
package-lock.json generated Normal file
View File

@ -0,0 +1,13 @@
{
"name": "iconify",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "iconify",
"hasInstallScript": true,
"license": "(Apache-2.0 OR GPL-2.0)",
"devDependencies": {}
}
}
}

View File

@ -11,11 +11,11 @@
"url": "https://github.com/iconify/iconify.git"
},
"scripts": {
"bootstrap": "node monorepo/bootstrap",
"clean": "node monorepo/clean",
"link": "node monorepo/link",
"unlink": "node monorepo/unlink",
"setup": "npm run clean && npm run bootstrap"
"install": "node monorepo/index install",
"clean": "node monorepo/index clean",
"link": "node monorepo/index link",
"unlink": "node monorepo/index unlink",
"reinstall": "node monorepo/index clean install"
},
"devDependencies": {}
}