Skip to content

Commit

Permalink
add publishNpmPkg script
Browse files Browse the repository at this point in the history
  • Loading branch information
spearwolf committed Oct 7, 2023
1 parent a0df1fe commit 7a4fd6f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spearwolf/twopoint5d",
"description": "a 2.5d graphics library built on three.js",
"description": "a library to create 2.5d realtime graphics and pixelart with three.js",
"version": "0.0.0",
"author": "Wolfger Schramm <[email protected]>",
"license": "Apache-2.0",
Expand All @@ -18,6 +18,7 @@
"lint": "nx run-many -t lint",
"test:affected": "nx affected -t test",
"clean": "nx run-many -t clean && rimraf dist",
"publishNpmPkg": "nx run-many -t publishNpmPkg",
"update": "npx npm-check --update",
"cbt": "run-s clean build test",
"lookbook": "nx dev lookbook",
Expand Down
8 changes: 8 additions & 0 deletions packages/twopoint5d/README-pkg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# @spearwolf/twopoint5d

_a library to create 2.5d realtime graphics and pixelart with three.js_

please see :octocat: [spearwolf/twopoint5d](https://github.com/spearwolf/twopoint5d) for full details

have fun! :rocket:

2 changes: 1 addition & 1 deletion packages/twopoint5d/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p align="center">
<img width="400" height="84" src="twopoint5d-logo.png">
<br>
<em>a 2.5d realtime gfx library built with three.js</em>
<em>a library to create 2.5d realtime graphics and pixelart with three.js</em>
</p>

---
Expand Down
3 changes: 2 additions & 1 deletion packages/twopoint5d/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"compile": "tsc -b tsconfig.build.json",
"bundle": "rollup -c rollup.config.mjs",
"clean": "rimraf build types dist ../../dist/packages/twopoint5d",
"makePackageJson": "node ../../scripts/makePackageJson.mjs"
"makePackageJson": "node ../../scripts/makePackageJson.mjs",
"publishNpmPkg": "node ../../scripts/publishNpmPkg.mjs dist"
},
"peerDependencies": {
"@spearwolf/eventize": "^3.0.0",
Expand Down
13 changes: 10 additions & 3 deletions packages/twopoint5d/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
"name": "twopoint5d",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"root": "packages/twopoint5d",
"sourceRoot": "packages/twopoint5d/src",
"sourceRoot": "{projectRoot}/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/twopoint5d/**/*.ts"]
"lintFilePatterns": ["{projectRoot}/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/twopoint5d/jest.config.ts",
"jestConfig": "{projectRoot}/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
Expand All @@ -25,6 +25,13 @@
"codeCoverage": true
}
}
},
"publishNpmPkg": {
"executor": "nx:run-script",
"dependsOn": ["^build", "build"],
"options": {
"script": "publishNpmPkg"
}
}
},
"tags": []
Expand Down
66 changes: 66 additions & 0 deletions scripts/publishNpmPkg.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {exec, execSync} from 'node:child_process';

const DRY_RUN = true || process.argv.includes('--dry-run');

const workspaceRoot = path.resolve(fileURLToPath(import.meta.url), '../../');
const projectRoot = path.resolve(process.cwd());
const packageRoot = path.resolve(projectRoot, process.argv[2]);
const pkgJson = JSON.parse(fs.readFileSync(path.resolve(packageRoot, 'package.json'), 'utf8'));

console.log('workspaceRoot:', workspaceRoot);
console.log('projectRoot:', projectRoot);
console.log('packageRoot:', packageRoot);
console.log('packageJson: ---');
console.dir(pkgJson);

if (pkgJson.version.endsWith('-dev')) {
console.warn('skip publishing, version', pkgJson.version, 'is marked as a *development* version');
process.exit(0);
}

exec(`npm show ${pkgJson.name} versions --json`, (error, stdout, stderr) => {
if (!error) {
const versions = JSON.parse(stdout);
console.log('already published versions: ---');
console.dir(versions);

if (versions.includes(pkgJson.version)) {
console.warn('skip publishing, version', pkgJson.version, 'is already released');
process.exit(0);
} else {
publishPackage(packageRoot);
}
} else if (stderr && stderr.toString().includes('npm ERR! code E404')) {
console.log('oh it looks like this is the first time to publish the package');
publishPackage(packageRoot);
} else {
console.error(`exec() panic: ${stderr}`);
process.exit(1);
}
});

function publishPackage(cwd, dryRun = DRY_RUN) {
copyFile(path.resolve(workspaceRoot, 'LICENSE'), path.resolve(cwd, 'LICENSE'));
copyFile(path.resolve(projectRoot, 'CHANGELOG.md'), path.resolve(cwd, 'CHANGELOG.md'));

const readmePkgPath = path.resolve(projectRoot, 'README-pkg.md');
const readmeDstPath = path.resolve(cwd, 'README.md');
if (fs.existsSync(readmePkgPath)) {
copyFile(readmePkgPath, readmeDstPath);
} else {
copyFile(path.resolve(projectRoot, 'README.md'), readmeDstPath);
}

execSync(`npm publish --access public${dryRun ? ' --dry-run' : ''}`, {cwd});

process.exit(0);
}

function copyFile(src, dst) {
if (fs.existsSync(src)) {
fs.copyFileSync(src, dst);
}
}

0 comments on commit 7a4fd6f

Please sign in to comment.