Skip to content

Commit

Permalink
chore: Migrate to pnpm (#202)
Browse files Browse the repository at this point in the history
Co-authored-by: folex <[email protected]>
  • Loading branch information
nahsi and folex authored Nov 8, 2023
1 parent 91c3701 commit f349454
Show file tree
Hide file tree
Showing 10 changed files with 1,252 additions and 65,387 deletions.
152 changes: 152 additions & 0 deletions .github/scripts/ci.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#! /usr/bin/env node

const fs = require("fs").promises;
const path = require("path");

function printUsage() {
console.log(
`Usage: "ci bump-version %postfix%"`,
);
}

let postfix;
const mode = process.argv[2];

function validateArgs() {
switch (mode) {
case "bump-version":
postfix = process.argv[3];
if (!postfix) {
printUsage();
return false;
}
return true;

default:
return false;
}
}

const PATH_TO_PACKAGES = "./src/aqua/";

async function getPackageJsonsRecursive(currentPath) {
return (
await Promise.all(
(await fs.readdir(currentPath, { withFileTypes: true }))
.filter(
(file) =>
file.name !== "node_modules" &&
file.name !== "integrations-tests" &&
(file.isDirectory() || file.name === "package.json"),
)
.map((file) =>
file.isDirectory()
? getPackageJsonsRecursive(path.join(currentPath, file.name))
: Promise.resolve([
path.join(process.cwd(), currentPath, file.name),
])
),
)
).flat();
}

async function getVersion(file) {
const content = await fs.readFile(file);
const json = JSON.parse(content);
return [json.name, json.version];
}

function processDep(obj, name, fn) {
if (!obj) {
return;
}

if (!obj[name]) {
return;
}

fn(obj, obj[name]);
}
async function getVersionsMap(allPackageJsons) {
return new Map(await Promise.all(allPackageJsons.map(getVersion)));
}

function getVersionForPackageOrThrow(versionsMap, packageName) {
const version = versionsMap.get(packageName);
if (!version) {
console.log("Failed to get version for package: ", packageName);
process.exit(1);
}
return version;
}

async function checkConsistency(file, versionsMap) {
console.log("Checking: ", file);
const content = await fs.readFile(file);
const json = JSON.parse(content);

for (const [name, versionInDep] of versionsMap) {
const check = (x, version) => {
if (version.includes("*")) {
return;
}

if (versionInDep !== version) {
console.log(
`Error, versions don't match: ${name}:${version} !== ${versionInDep}`,
file,
);
process.exit(1);
}
};
processDep(json.dependencies, name, check);
processDep(json.devDependencies, name, check);
}
}

async function bumpVersions(file, versionsMap) {
console.log("Updating: ", file);
const content = await fs.readFile(file);
const json = JSON.parse(content);

// bump dependencies
for (const [name, version] of versionsMap) {
const update = (x) => (x[name] = `${version}-${postfix}`);
processDep(json.dependencies, name, update);
processDep(json.devDependencies, name, update);
}

// also bump version in package itself
const version = getVersionForPackageOrThrow(versionsMap, json.name);
json.version = `${version}-${postfix}`;

const newContent = JSON.stringify(json, undefined, 4) + "\n";
await fs.writeFile(file, newContent);
}

async function processPackageJsons(allPackageJsons, versionsMap, fn) {
await Promise.all(allPackageJsons.map((x) => fn(x, versionsMap)));
}

async function run() {
if (!validateArgs()) {
printUsage();
process.exit(1);
}

const packageJsons = await getPackageJsonsRecursive(PATH_TO_PACKAGES);
const versionsMap = await getVersionsMap(packageJsons);

// always check consistency
console.log("Checking versions consistency...");
await processPackageJsons(packageJsons, versionsMap, checkConsistency);
console.log("Versions are consistent");

if (mode === "bump-version") {
console.log("Adding postfix: ", postfix);
await processPackageJsons(packageJsons, versionsMap, bumpVersions);
console.log("Done");
}
}

run();
11 changes: 8 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ jobs:
repository: fluencelabs/spell
ref: ${{ github.ref }}

- name: Setup node with self-hosted registry
- uses: pnpm/[email protected]
with:
version: 8

- name: Setup node with self-hosted npm registry
uses: actions/setup-node@v3
with:
node-version: "18"
registry-url: "https://npm.fluence.dev"
cache: "pnpm"

- run: npm install
- run: pnpm install
working-directory: src/aqua/installation-spell

- run: npm run check
- run: pnpm run check
working-directory: src/aqua/installation-spell
52 changes: 24 additions & 28 deletions .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ on:
value: ${{ jobs.snapshot.outputs.cargo-version }}
spell-version:
description: "@fluencelabs/spell version"
value: ${{ jobs.snapshot.outputs.spell-version }}
value: ${{ fromJson(jobs.snapshot.outputs.snapshots)['spell'] }}
installation-spell-version:
description: "@fluencelabs/installation-spell version"
value: ${{ jobs.snapshot.outputs.installation-spell-version }}
value: ${{ fromJson(jobs.snapshot.outputs.snapshots)['installation-spell'] }}
spell-snapshots:
description: "spell snapshots"
value: ${{ jobs.snapshot.outputs.snapshots }}

env:
CARGO_UNSTABLE_SPARSE_REGISTRY: true
Expand All @@ -37,8 +40,7 @@ jobs:

outputs:
cargo-version: "${{ steps.cargo-snapshot.outputs.version }}"
spell-version: "${{ steps.spell-snapshot.outputs.version }}"
installation-spell-version: "${{ steps.installation-spell-snapshot.outputs.version }}"
snapshots: "${{ steps.snapshot.outputs.snapshots }}"

permissions:
contents: read
Expand Down Expand Up @@ -105,12 +107,6 @@ jobs:
working-directory: ./src/spell/modules/spell/spell
run: ./test.sh

- name: Setup node with self-hosted npm registry
uses: actions/setup-node@v3
with:
node-version: "18"
registry-url: "https://npm.fluence.dev"

- name: Publish crate snapshots
if: inputs.snapshot == true
id: cargo-snapshot
Expand All @@ -119,28 +115,28 @@ jobs:
id: ${{ steps.version.outputs.id }}
path: src/spell/modules/spell

- name: Run npm i in spell
if: inputs.snapshot == true
run: npm i
working-directory: src/aqua/spell
- uses: pnpm/[email protected]
with:
version: 8

- name: Run npm i in installation-spell
if: inputs.snapshot == true
run: npm i
working-directory: src/aqua/installation-spell
- name: Setup node with self-hosted npm registry
uses: actions/setup-node@v3
with:
node-version: "18"
registry-url: "https://npm.fluence.dev"
cache: "pnpm"

- name: Publish spell snapshot
- name: Run pnpm i
if: inputs.snapshot == true
id: spell-snapshot
uses: fluencelabs/github-actions/npm-publish-snapshot@main
with:
working-directory: src/aqua/spell
id: ${{ steps.version.outputs.id }}
run: pnpm -r --no-frozen-lockfile i

- name: Set package version
run: node .github/scripts/ci.cjs bump-version ${{ steps.version.outputs.id }}

- name: Publish installation-spell snapshot
- name: Publish snapshots
if: inputs.snapshot == true
id: installation-spell-snapshot
uses: fluencelabs/github-actions/npm-publish-snapshot@main
id: snapshot
uses: fluencelabs/github-actions/pnpm-publish-snapshot@main
with:
working-directory: src/aqua/installation-spell
id: ${{ steps.version.outputs.id }}
set-version: false
Loading

0 comments on commit f349454

Please sign in to comment.