Skip to content

Commit

Permalink
👷 (packages) [DSDK-262]: Build packages and ready them for publishing (…
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman authored Apr 8, 2024
2 parents d100f8f + f94a10d commit daa0729
Show file tree
Hide file tree
Showing 36 changed files with 1,527 additions and 820 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: publish npm packages
on:
push:
branches:
- develop

env:
FORCE_COLOR: "1"

jobs:
publish:
environment: Production
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-toolchain-composite

- name: install dependencies
run: pnpm install

- name: build libraries
run: pnpm build

- name: publish
uses: changesets/action@v1
with:
publish: pnpm release
env:
GITHUB_TOKEN: ${{ github.token }}
NPM_TOKEN: ${{ secrets.NPMJS_TOKEN }}
9 changes: 7 additions & 2 deletions .github/workflows/version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ jobs:
- name: install dependencies
run: pnpm install

- name: create release pull request
- name: build libraries
run: pnpm build

- name: create release pull request or publish
uses: changesets/action@v1
with:
version: pnpm bump
commit: "đź”– (release): versioning packages"
title: "đź”– (release) [NO-ISSUE]: versioning packages"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ github.token }}
2 changes: 1 addition & 1 deletion danger/dangerfile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { danger, fail } from "danger";

const branchRegex =
/^(feature|bugfix|support|chore|doc|refacto)\/((DSDK|dsdk)-[0-9]+|NO-ISSUE|no-issue)\-.*/;
/^(feature|bugfix|support|chore|doc|refacto)\/((dsdk)-[0-9]+|no-issue)\-.*/i;
if (!branchRegex.test(danger.github.pr.head.ref)) {
fail(
"Please fix the PR branch name to match the convention, see [this documentation](https://ledgerhq.atlassian.net/wiki/spaces/WXP/pages/4527358147/DSDK+TS+Git+-+Github+conventions)"
Expand Down
33 changes: 15 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"apps/*"
],
"scripts": {
"build": "turbo build --log-order=grouped",
"dev": "turbo dev --log-order=grouped",
"build": "turbo run build --log-order=grouped",
"dev": "turbo run dev --filter=...device-sdk-sample",
"lint": "turbo run lint --log-order=grouped",
"lint:fix": "turbo run lint:fix --log-order=grouped",
"prettier": "turbo run prettier --log-order=grouped",
Expand All @@ -32,28 +32,25 @@
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.4",
"concurrently": "^8.2.2",
"danger": "^11.3.1",
"eslint": "^8.56.0",
"gitmoji-cli": "^9.1.0",
"esbuild": "^0.20.2",
"esbuild-plugin-d.ts": "^1.2.3",
"eslint": "^8.57.0",
"gitmoji-cli": "^9.2.0",
"hygen": "^6.2.11",
"jest": "^29.7.0",
"lint-staged": "^15.2.0",
"prettier": "^3.1.1",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"turbo": "^1.12.3",
"typescript": "^5.3.3",
"ts-jest": "^29.1.2",
"tsc-alias": "^1.8.8",
"turbo": "^1.13.2",
"typescript": "^5.4.4",
"zx": "^7.2.3"
},
"lint-staged": {
"*.ts": [
"eslint --fix",
"prettier --write"
]
},
"packageManager": "[email protected]",
"packageManager": "[email protected]",
"engines": {
"node": ">=18"
}
Expand Down
14 changes: 14 additions & 0 deletions packages/config/esbuild/config/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const buildConfig = (config, packageJson) => ({
sourcemap: true,
treeShaking: true,
color: true,
bundle: true,
minify: true,
...config,
plugins: [...(config.plugins || [])],
external: Object.keys(packageJson.dependencies || {}).concat(
Object.keys(packageJson.peerDependencies || {})
),
});

module.exports = buildConfig;
9 changes: 9 additions & 0 deletions packages/config/esbuild/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const alias = require("./plugins/alias");
const copy = require("./plugins/copy");
const buildConfig = require("./config/common");

module.exports = {
alias,
copy,
buildConfig,
};
5 changes: 5 additions & 0 deletions packages/config/esbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@ledgerhq/esbuild-tools",
"main": "index.js",
"private": true
}
66 changes: 66 additions & 0 deletions packages/config/esbuild/plugins/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const path = require("path");

/**
* Alias Plugin for Esbuild (copied from Ledger Live)
*
* Replace a string by another string in the import path, should match the same
* config as the tsconfig.json paths.
* @param {*} mappings
* @example { "@root": path.join(__dirname, "..") } will replace root by the specicified path
* @returns void
*/
module.exports = (mappings) => ({
name: "Alias",
setup(build) {
Object.entries(mappings).forEach(([filter, mappings]) => {
if (!Array.isArray(mappings)) {
mappings = [mappings];
}

const resolveCallback = async function (args) {
if (args.resolveDir === "") {
return; // Ignore unresolvable paths
}

const errors = [];

for (const mapping of mappings) {
const rawMappedPath = args.path.replace(filter, mapping);
const relativeMappedPath = path
.relative(args.resolveDir, rawMappedPath)
// Fixes windows paths
.replace(new RegExp("\\\\+", "g"), "/");

const mappedPath = relativeMappedPath.startsWith(".")
? relativeMappedPath
: `./${relativeMappedPath}`;

let result = await build.resolve(mappedPath, {
kind: "import-statement",
resolveDir: args.resolveDir,
});

if (result.errors.length > 0) {
errors.push(...result.errors);
result = await build.resolve(rawMappedPath, {
kind: "import-statement",
resolveDir: args.resolveDir,
});

if (result.errors.length > 0) {
errors.push(...result.errors);
continue;
}
}

return result;
}

return { errors };
};

build.onResolve({ filter: new RegExp(`^${filter}$`) }, resolveCallback);
build.onResolve({ filter: new RegExp(`^${filter}/.+`) }, resolveCallback);
});
},
});
42 changes: 42 additions & 0 deletions packages/config/esbuild/plugins/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require("fs");
const path = require("path");

// Copy a folder and its contents recursively.
function copyFolderRecursivelySync(source, target, options = {}) {
if (!fs.existsSync(target)) {
fs.mkdirSync(target, { recursive: true, ...options });
}

if (fs.statSync(source).isDirectory()) {
const files = fs.readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);
if (fs.statSync(curSource).isDirectory()) {
copyFolderRecursivelySync(curSource, path.join(target, file));
} else {
fs.copyFileSync(curSource, path.join(target, path.basename(file)));
}
});
}
}

module.exports = ({ patterns, options = {} }) => {
return {
name: "Copy",
setup(build) {
build.onEnd(({ errors }) => {
if (errors.length && !options.skipOnError) {
return;
}

const targetBase =
build.initialOptions.outdir ||
path.dirname(build.initialOptions.outfile);

patterns.forEach(({ from, to }) => {
copyFolderRecursivelySync(from, path.join(targetBase, to), options);
});
});
},
};
};
1 change: 0 additions & 1 deletion packages/config/typescript/sdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"declaration": true,
"declarationMap": true,
"skipLibCheck": false,
"baseUrl": ".",
"strict": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,
Expand Down
15 changes: 9 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
"name": "@ledgerhq/device-sdk-core",
"version": "0.0.1",
"license": "MIT",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
".": {
"require": "./lib/cjs/index.js",
"import": "./lib/esm/index.js"
}
},
"files": [
"./lib"
],
"scripts": {
"build": "rimraf lib && tsc -p tsconfig.prod.json && tsc-alias -p tsconfig.prod.json",
"build": "rimraf lib && zx scripts/build.mjs",
"dev": "tsc && (concurrently \"tsc -w -p tsconfig.prod.json\" \"tsc-alias -w -p tsconfig.prod.json\")",
"lint": "eslint --cache --ext .ts \"src\"",
"lint:fix": "eslint --cache --fix --ext .ts \"src\"",
Expand All @@ -31,15 +35,14 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@ledgerhq/esbuild-tools": "workspace:*",
"@ledgerhq/eslint-config-dsdk": "workspace:*",
"@ledgerhq/jest-config-dsdk": "workspace:*",
"@ledgerhq/prettier-config-dsdk": "workspace:*",
"@ledgerhq/tsconfig-dsdk": "workspace:*",
"@types/semver": "^7.5.6",
"@types/uuid": "^9.0.8",
"@types/w3c-web-hid": "^1.0.6",
"concurrently": "^8.2.2",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.8"
"ts-node": "^10.9.2"
}
}
65 changes: 65 additions & 0 deletions packages/core/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env zx
import { build } from "esbuild";
import { alias, buildConfig } from "@ledgerhq/esbuild-tools";
import path from "node:path";
import dtsPlugin from "esbuild-plugin-d.ts";
import pkg from "../package.json" assert { type: "json" };

const root = path.join(__dirname, "..");
const srcRoot = path.join(root, "src");
const outdir = path.join(root, "lib");
const tsconfigEsm = path.join(root, "tsconfig.esm.json");
const tsconfigCjs = path.join(root, "tsconfig.cjs.json");

const conf = {
entryPoints: [path.join(root, "index.ts")],
plugins: [
alias({
"@root": root,
"@internal": path.join(srcRoot, "internal"),
"@api": path.join(srcRoot, "api"),
}),
],
};

const common = buildConfig(conf, pkg);

const buildEsm = async () => {
const config = {
...common,
outdir: path.join(outdir, "esm"),
format: "esm",
tsconfig: tsconfigEsm,
plugins: [
...common.plugins,
dtsPlugin({
tsconfig: tsconfigEsm,
}),
],
};

await build(config);
};

const builCjs = async () => {
const config = {
...common,
outdir: path.join(outdir, "cjs"),
format: "cjs",
tsconfig: tsconfigCjs,
plugins: [
...common.plugins,
dtsPlugin({
tsconfig: tsconfigCjs,
}),
],
};

await build(config);
};

const run = async () => await Promise.all([buildEsm(), builCjs()]);

run().catch((e) => {
console.error(e);
});
10 changes: 10 additions & 0 deletions packages/core/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"exclude": ["src/**/*.test.ts", "jest.*.ts"],
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./lib/cjs",
"declarationDir": "./lib/cjs"
}
}
9 changes: 9 additions & 0 deletions packages/core/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"exclude": ["src/**/*.test.ts", "jest.*.ts"],
"compilerOptions": {
"module": "esnext",
"outDir": "./lib/esm",
"declarationDir": "./lib/esm"
}
}
Loading

0 comments on commit daa0729

Please sign in to comment.