ci: remove gulp and add auto publishing

This commit is contained in:
Johannes Loher 2021-12-20 06:10:46 +01:00
parent b2a5116cab
commit b54561eb3e
15 changed files with 2170 additions and 265 deletions

View file

@ -2,4 +2,7 @@
#
# SPDX-License-Identifier: MIT
dist
/dist
/.pnp.cjs
/.pnp.loader.mjs
/.yarn/

View file

@ -4,13 +4,14 @@
module.exports = {
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: 2022,
sourceType: 'module',
},
env: {
browser: true,
es2020: true,
es2021: true,
jquery: true,
},
extends: ['eslint:recommended', '@typhonjs-fvtt/eslint-config-foundry.js/0.8.0', 'plugin:prettier/recommended'],
@ -27,9 +28,11 @@ module.exports = {
overrides: [
{
files: ['./*.js'],
files: ['./*.js', './tools/**/*'],
env: {
node: true,
browser: false,
jquery: false,
},
},
],

View file

@ -8,7 +8,9 @@ image: node:lts
stages:
- test
- build
- prepare-release
- release
- publish
cache: &global_cache
paths:
@ -46,6 +48,33 @@ build:
- darkness-dependent-vision
expire_in: 1 week
publish-artifacts:
stage: prepare-release
image: alpine:latest
before_script:
- apk update
- apk add zip curl
script: |
zip -r darkness-dependent-vision.zip darkness-dependent-vision/*
curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file darkness-dependent-vision.zip "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/darkness-dependent-vision/$CI_COMMIT_TAG/darkness-dependent-vision.zip"
curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file darkness-dependent-vision/module.json "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/darkness-dependent-vision/$CI_COMMIT_TAG/module.json"
rules:
- if: '$CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/'
changelog:
stage: prepare-release
before_script:
- yarn install --immutable
script:
- yarn changelog
cache:
<<: *global_cache
artifacts:
paths:
- CHANGELOG.md
rules:
- if: '$CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/'
.release-template: &release-template
stage: release
before_script:
@ -65,11 +94,9 @@ build:
RELEASE_VERSION=$(jq -r '.version' < package.json)
git add package.json src/module.json
git --no-pager diff
git commit -m "release version ${RELEASE_VERSION}"
git tag -f latest
git commit -m "chore(release): ${RELEASE_VERSION}"
git tag -f ${RELEASE_VERSION}
git push origin ci-processing:${CI_BUILD_REF_NAME}
git push origin latest -f
git push origin ci-processing:${CI_BUILD_REF_NAME} -o ci.skip
git push origin ${RELEASE_VERSION}
only:
- master
@ -89,3 +116,45 @@ release-major:
variables:
RELEASE_TYPE: major
<<: *release-template
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
script:
- echo 'release job'
rules:
- if: '$CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/'
release:
tag_name: $CI_COMMIT_TAG
description: './CHANGELOG.md'
assets:
links:
- name: 'darkness-dependent-vision.zip'
url: '${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/darkness-dependent-vision/$CI_COMMIT_TAG/darkness-dependent-vision.zip'
filepath: /darkness-dependent-vision.zip
link_type: package
- name: 'module.json'
url: '${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/darkness-dependent-vision/$CI_COMMIT_TAG/module.json'
filepath: /module.json
link_type: other
publish-latest-manifest:
stage: publish
image: alpine:latest
before_script:
- apk update
- apk add zip curl
script: |
curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file darkness-dependent-vision/module.json "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/darkness-dependent-vision/latest/module.json"
rules:
- if: '$CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/'
publish-to-foundry-admin:
stage: publish
image: johannesloher/foundry-publish
variables:
FVTT_MANIFEST_PATH: darkness-dependent-vision/module.json
FVTT_MANIFEST_URL: ${CI_PROJECT_URL}/-/releases/${CI_COMMIT_TAG}/downloads/module.json
script: foundry-publish
rules:
- if: '$CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/'

4
.husky/commit-msg Executable file
View file

@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
yarn run commitlint --edit "$1"

View file

@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2021 Johannes Loher
SPDX-License-Identifier: MIT

View file

@ -2,5 +2,8 @@
#
# SPDX-License-Identifier: MIT
dist
/dist
package-lock.json
/.pnp.cjs
/.pnp.loader.mjs
/.yarn/

5
commitlint.config.js Normal file
View file

@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
module.exports = { extends: ['@commitlint/config-conventional'] };

View file

@ -1,202 +0,0 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
const { rollup } = require('rollup');
const argv = require('yargs').argv;
const chalk = require('chalk');
const fs = require('fs-extra');
const gulp = require('gulp');
const path = require('path');
const rollupConfig = require('./rollup.config');
const semver = require('semver');
/********************/
/* CONFIGURATION */
/********************/
const name = path.basename(path.resolve('.'));
const sourceDirectory = './src';
const distDirectory = './dist';
const sourceFileExtension = 'js';
const staticFiles = ['lang', 'templates', 'module.json'];
const getDownloadURL = (version) =>
`https://git.f3l.de/ghost/darkness-dependent-vision/-/jobs/artifacts/${version}/download?job=build`;
/********************/
/* BUILD */
/********************/
/**
* Build the distributable JavaScript code
*/
async function buildCode() {
const build = await rollup({ input: rollupConfig.input, plugins: rollupConfig.plugins });
return build.write(rollupConfig.output);
}
/**
* Copy static files
*/
async function copyFiles() {
for (const file of staticFiles) {
if (fs.existsSync(`${sourceDirectory}/${file}`)) {
await fs.copy(`${sourceDirectory}/${file}`, `${distDirectory}/${file}`);
}
}
}
/**
* Watch for changes for each build step
*/
function buildWatch() {
gulp.watch(`${sourceDirectory}/**/*.${sourceFileExtension}`, { ignoreInitial: false }, buildCode);
gulp.watch(
staticFiles.map((file) => `${sourceDirectory}/${file}`),
{ ignoreInitial: false },
copyFiles,
);
}
/********************/
/* CLEAN */
/********************/
/**
* Remove built files from `dist` folder while ignoring source files
*/
async function clean() {
const files = [...staticFiles, 'module'];
console.log(' ', chalk.yellow('Files to clean:'));
console.log(' ', chalk.blueBright(files.join('\n ')));
for (const filePath of files) {
await fs.remove(`${distDirectory}/${filePath}`);
}
}
/********************/
/* LINK */
/********************/
/**
* Get the data path of Foundry VTT based on what is configured in `foundryconfig.json`
*/
function getDataPath() {
const config = fs.readJSONSync('foundryconfig.json');
if (config?.dataPath) {
if (!fs.existsSync(path.resolve(config.dataPath))) {
throw new Error('User Data path invalid, no Data directory found');
}
return path.resolve(config.dataPath);
} else {
throw new Error('No User Data path defined in foundryconfig.json');
}
}
/**
* Link build to User Data folder
*/
async function linkUserData() {
let destinationDirectory;
if (fs.existsSync(path.resolve(sourceDirectory, 'module.json'))) {
destinationDirectory = 'modules';
} else {
throw new Error(`Could not find ${chalk.blueBright('module.json')}`);
}
const linkDirectory = path.resolve(getDataPath(), destinationDirectory, name);
if (argv.clean || argv.c) {
console.log(chalk.yellow(`Removing build in ${chalk.blueBright(linkDirectory)}.`));
await fs.remove(linkDirectory);
} else if (!fs.existsSync(linkDirectory)) {
console.log(chalk.green(`Linking dist to ${chalk.blueBright(linkDirectory)}.`));
await fs.ensureDir(path.resolve(linkDirectory, '..'));
await fs.symlink(path.resolve(distDirectory), linkDirectory);
}
}
/********************/
/* VERSIONING */
/********************/
/**
* Get the contents of the manifest file as object.
*/
function getManifest() {
const manifestPath = `${sourceDirectory}/module.json`;
if (fs.existsSync(manifestPath)) {
return {
file: fs.readJSONSync(manifestPath),
name: 'module.json',
};
}
}
/**
* Get the target version based on on the current version and the argument passed as release.
*/
function getTargetVersion(currentVersion, release) {
if (['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'].includes(release)) {
return semver.inc(currentVersion, release);
} else {
return semver.valid(release);
}
}
/**
* Update version and download URL.
*/
function bumpVersion(cb) {
const packageJson = fs.readJSONSync('package.json');
const manifest = getManifest();
if (!manifest) cb(Error(chalk.red('Manifest JSON not found')));
try {
const release = argv.release || argv.r;
const currentVersion = packageJson.version;
if (!release) {
return cb(Error('Missing release type'));
}
const targetVersion = getTargetVersion(currentVersion, release);
if (!targetVersion) {
return cb(new Error(chalk.red('Error: Incorrect version arguments')));
}
if (targetVersion === currentVersion) {
return cb(new Error(chalk.red('Error: Target version is identical to current version')));
}
console.log(`Updating version number to '${targetVersion}'`);
packageJson.version = targetVersion;
fs.writeJSONSync('package.json', packageJson, { spaces: 2 });
manifest.file.version = targetVersion;
manifest.file.download = getDownloadURL(targetVersion);
fs.writeJSONSync(`${sourceDirectory}/${manifest.name}`, manifest.file, { spaces: 2 });
return cb();
} catch (err) {
cb(err);
}
}
const execBuild = gulp.parallel(buildCode, copyFiles);
exports.build = gulp.series(clean, execBuild);
exports.watch = buildWatch;
exports.clean = clean;
exports.link = linkUserData;
exports.bumpVersion = bumpVersion;

View file

@ -19,20 +19,28 @@
}
],
"scripts": {
"build": "gulp build",
"build:watch": "gulp watch",
"link-project": "gulp link",
"clean": "gulp clean",
"clean:link": "gulp link --clean",
"bump-version": "gulp bumpVersion",
"lint": "eslint --ext .js .",
"lint:fix": "eslint --ext .js --fix .",
"format": "prettier --write \"./**/*.(js|json|css|yml)\"",
"postinstall": "husky install"
"build": "run-s clean:files build:files",
"build:files": "rollup -c",
"watch": "rollup -c -w",
"link-package": "node ./tools/link-package.mjs",
"clean": "run-p clean:files clean:link",
"clean:files": "rimraf dist",
"clean:link": "node ./tools/link-package.mjs --clean",
"lint": "eslint --ext .ts,.js,.cjs,.mjs .",
"lint:fix": "eslint --ext .ts,.js,.cjs,.mjs --fix .",
"format": "prettier --write \"./**/*.(ts|js|cjs|mjs|json|scss|yml)\"",
"typecheck": "tsc --noEmit",
"bump-version": "node ./tools/bump-version.js",
"postinstall": "husky install",
"changelog": "conventional-changelog -p conventionalcommits -o CHANGELOG.md -r 2"
},
"devDependencies": {
"@commitlint/cli": "15.0.0",
"@commitlint/config-conventional": "15.0.0",
"@guanghechen/rollup-plugin-copy": "1.8.5",
"@typhonjs-fvtt/eslint-config-foundry.js": "0.8.0",
"chalk": "4.1.2",
"conventional-changelog-cli": "^2.1.1",
"conventional-changelog-conventionalcommits": "^4.6.1",
"eslint": "8.5.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-prettier": "4.0.0",
@ -40,7 +48,9 @@
"gulp": "4.0.2",
"husky": "7.0.4",
"lint-staged": "12.1.2",
"npm-run-all": "4.1.5",
"prettier": "2.5.1",
"rimraf": "3.0.2",
"rollup": "2.61.1",
"rollup-plugin-sourcemaps": "0.6.3",
"rollup-plugin-terser": "7.0.2",
@ -48,7 +58,8 @@
"yargs": "17.3.0"
},
"lint-staged": {
"*.(js)": "eslint --fix",
"*.(js|mjs|cjs)": "eslint --fix",
"*.(json|css|yml)": "prettier --write"
}
},
"packageManager": "yarn@3.1.1"
}

View file

@ -2,20 +2,31 @@
//
// SPDX-License-Identifier: MIT
const sourcemaps = require('rollup-plugin-sourcemaps');
const { terser } = require('rollup-plugin-terser');
import copy from '@guanghechen/rollup-plugin-copy';
import sourcemaps from 'rollup-plugin-sourcemaps';
import { terser } from 'rollup-plugin-terser';
import { distDirectory, name, sourceDirectory } from './tools/const.mjs';
const staticFiles = ['module.json', 'lang', 'templates'];
const isProduction = process.env.NODE_ENV === 'production';
/**
* @type {import('rollup').RollupOptions}
*/
const config = {
input: 'src/module/darkness-dependent-vision.js',
input: { [`module/${name}`]: `${sourceDirectory}/module/${name}.js` },
output: {
dir: 'dist/module',
dir: distDirectory,
format: 'es',
sourcemap: true,
assetFileNames: '[name].[ext]',
},
plugins: [sourcemaps(), process.env.NODE_ENV === 'production' && terser({ ecma: 2020, keep_fnames: true })],
plugins: [
sourcemaps(),
copy({
targets: [{ src: staticFiles.map((file) => `${sourceDirectory}/${file}`), dest: distDirectory }],
}),
isProduction && terser({ ecma: 2020, keep_fnames: true }),
],
};
module.exports = config;
export default config;

View file

@ -26,10 +26,10 @@
}
],
"url": "https://git.f3l.de/ghost/darkness-dependent-vision",
"manifest": "https://git.f3l.de/ghost/darkness-dependent-vision/-/raw/latest/src/module.json?inline=false",
"manifest": "https://git.f3l.de/api/v4/projects/ghost%2Fdarkness-dependent-vision/packages/generic/darkness-dependent-vision/latest/module.json",
"download": "https://git.f3l.de/ghost/darkness-dependent-vision/-/jobs/artifacts/0.2.2/download?job=build",
"license": "https://git.f3l.de/ghost/darkness-dependent-vision#licensing",
"readme": "https://git.f3l.de/ghost/darkness-dependent-vision/-/blob/master/README.md",
"bugs": "https://git.f3l.de/ghost/darkness-dependent-vision/-/issues",
"changelog": "https://git.f3l.de/ghost/darkness-dependent-vision/-/releases"
"changelog": "https://git.f3l.de/ghost/darkness-dependent-vision/-/releases/0.2.0"
}

85
tools/bump-version.mjs Normal file
View file

@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import fs from 'fs-extra';
import path from 'node:path';
import semver from 'semver';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { sourceDirectory } from './const.mjs';
const getDownloadURL = (version) =>
`https://git.f3l.de/ghost/darkness-dependent-vision/-/releases/${version}/downloads/darkness-dependent-vision.zip`;
const getChangelogURL = (version) => `https://git.f3l.de/ghost/darkness-dependent-vision/-/releases/${version}`;
/**
* Get the contents of the manifest file as object.
* @returns {{file: unknown, name: string}} An object describing the manifest
*/
function getManifest() {
const manifestPath = path.join(sourceDirectory, 'module.json');
if (fs.existsSync(manifestPath)) {
return {
file: fs.readJSONSync(manifestPath),
name: 'module.json',
};
}
}
/**
* Get the target version based on on the current version and the argument passed as release.
* @param {string} currentVersion The current version
* @param {semver.ReleaseType | string} release Either a semver release type or a valid semver version
* @returns {string | null} The target version
*/
function getTargetVersion(currentVersion, release) {
if (['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'].includes(release)) {
return semver.inc(currentVersion, release);
} else {
return semver.valid(release);
}
}
/**
* Update version and download URL.
* @param {semver.ReleaseType | string} release Either a semver release type or a valid semver version
*/
function bumpVersion(release) {
if (!release) {
throw new Error('Missing release type');
}
const packageJson = fs.readJSONSync('package.json');
const manifest = getManifest();
if (!manifest) throw new Error('Manifest JSON not found');
const currentVersion = packageJson.version;
const targetVersion = getTargetVersion(currentVersion, release);
if (!targetVersion) {
throw new Error('Incorrect version arguments');
}
if (targetVersion === currentVersion) {
throw new Error('Target version is identical to current version');
}
console.log(`Bumping version number to '${targetVersion}'`);
packageJson.version = targetVersion;
fs.writeJSONSync('package.json', packageJson, { spaces: 4 });
manifest.file.version = targetVersion;
manifest.file.download = getDownloadURL(targetVersion);
manifest.file.changelog = getChangelogURL(targetVersion);
fs.writeJSONSync(path.join(sourceDirectory, manifest.name), manifest.file, { spaces: 4 });
}
const argv = yargs(hideBin(process.argv)).usage('Usage: $0').option('release', {
alias: 'r',
type: 'string',
demandOption: true,
description: 'Either a semver release type or a valid semver version',
}).argv;
const release = argv.r;
bumpVersion(release);

9
tools/const.mjs Normal file
View file

@ -0,0 +1,9 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
export const name = 'darkness-dependent-vision';
export const sourceDirectory = './src';
export const distDirectory = './dist';
export const destinationDirectory = 'modules';
export const foundryconfigFile = './foundryconfig.json';

55
tools/link-package.mjs Normal file
View file

@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import fs from 'fs-extra';
import path from 'node:path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { destinationDirectory, distDirectory, foundryconfigFile, name, sourceDirectory } from './const.mjs';
/**
* Get the data path of Foundry VTT based on what is configured in the {@link foundryconfigFile}.
*/
function getDataPath() {
const config = fs.readJSONSync(foundryconfigFile);
if (config?.dataPath) {
if (!fs.existsSync(path.resolve(config.dataPath))) {
throw new Error('User data path invalid, no Data directory found');
}
return path.resolve(config.dataPath);
} else {
throw new Error(`No user data path defined in ${foundryconfigFile}`);
}
}
/**
* Link the built package to the user data folder.
* @param {boolean} clean Whether to remove the link instead of creating it
*/
async function linkPackage(clean) {
if (!fs.existsSync(path.resolve(sourceDirectory, 'module.json'))) {
throw new Error('Could not find module.json');
}
const linkDirectory = path.resolve(getDataPath(), 'Data', destinationDirectory, name);
if (clean) {
console.log(`Removing link to built package at ${linkDirectory}.`);
await fs.remove(linkDirectory);
} else if (!fs.existsSync(linkDirectory)) {
console.log(`Linking built package to ${linkDirectory}.`);
await fs.ensureDir(path.resolve(linkDirectory, '..'));
await fs.symlink(path.resolve('.', distDirectory), linkDirectory);
}
}
const argv = yargs(hideBin(process.argv)).usage('Usage: $0').option('clean', {
alias: 'c',
type: 'boolean',
default: false,
description: 'Remove the link instead of creating it',
}).argv;
const clean = argv.c;
await linkPackage(clean);

1912
yarn.lock

File diff suppressed because it is too large Load diff