Skip to content

Commit 54293ac

Browse files
committed
fix some issues; migrate to bun'
1 parent cf3d5c6 commit 54293ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1356
-760
lines changed

.github/workflows/release.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
name: Build obsidian plugin
1+
name: Build Obsidian Plugin
2+
3+
# adapted from https://github.com/argenos/nldates-obsidian/blob/master/.github/workflows/release.yml
24

35
on:
46
push:
@@ -15,15 +17,14 @@ jobs:
1517

1618
steps:
1719
- uses: actions/checkout@v3
18-
- name: Use Node.js
19-
uses: actions/setup-node@v3
20+
- uses: oven-sh/setup-bun@v1
2021
with:
21-
node-version: '16.x' # You might need to adjust this value to your own version
22+
bun-version: latest
2223
- name: Build
2324
id: build
2425
run: |
25-
yarn
26-
yarn run build --if-present
26+
bun install
27+
bun run build
2728
mkdir ${{ env.PLUGIN_NAME }}
2829
cp main.js manifest.json styles.css ${{ env.PLUGIN_NAME }}
2930
zip -r ${{ env.PLUGIN_NAME }}.zip ${{ env.PLUGIN_NAME }}

automation/build/buildBanner.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import manifest from '../../manifest.json' assert { type: 'json' };
2+
3+
export function getBuildBanner(buildType: string, getVersion: (version: string) => string) {
4+
return `/*
5+
-------------------------------------------
6+
${manifest.name} - ${buildType}
7+
-------------------------------------------
8+
By: ${manifest.author} (${manifest.authorUrl})
9+
Time: ${new Date().toUTCString()}
10+
Version: ${getVersion(manifest.version)}
11+
-------------------------------------------
12+
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
13+
if you want to view the source, please visit the github repository of this plugin
14+
-------------------------------------------
15+
MIT License
16+
17+
Copyright (c) ${new Date().getFullYear()} ${manifest.author}
18+
19+
Permission is hereby granted, free of charge, to any person obtaining a copy
20+
of this software and associated documentation files (the "Software"), to deal
21+
in the Software without restriction, including without limitation the rights
22+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23+
copies of the Software, and to permit persons to whom the Software is
24+
furnished to do so, subject to the following conditions:
25+
26+
The above copyright notice and this permission notice shall be included in all
27+
copies or substantial portions of the Software.
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
SOFTWARE.
36+
*/
37+
`;
38+
}

esbuild.config.mjs renamed to automation/build/esbuild.config.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
import esbuild from 'esbuild';
21
import builtins from 'builtin-modules';
2+
import esbuild from 'esbuild';
33
import esbuildSvelte from 'esbuild-svelte';
44
import sveltePreprocess from 'svelte-preprocess';
5-
import manifest from './manifest.json' assert { type: 'json' };
5+
import { getBuildBanner } from 'build/buildBanner';
66

7-
const banner = `/*
8-
-------------------------------------------
9-
${manifest.name} - Release Build
10-
-------------------------------------------
11-
By: ${manifest.author} (${manifest.authorUrl})
12-
Time: ${new Date().toUTCString()}
13-
Version: ${manifest.version}
14-
-------------------------------------------
15-
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
16-
if you want to view the source, please visit the github repository of this plugin
17-
*/
18-
`;
7+
const banner = getBuildBanner('Release Build', version => version);
198

209
const build = await esbuild.build({
2110
banner: {
@@ -47,14 +36,22 @@ const build = await esbuild.build({
4736
outfile: 'main.js',
4837
minify: true,
4938
metafile: true,
39+
define: {
40+
MB_GLOBAL_CONFIG_DEV_BUILD: 'false',
41+
},
5042
plugins: [
5143
esbuildSvelte({
52-
compilerOptions: { css: 'injected' },
44+
compilerOptions: { css: 'injected', dev: false, sveltePath: 'svelte' },
5345
preprocess: sveltePreprocess(),
5446
filterWarnings: warning => {
5547
// we don't want warnings from node modules that we can do nothing about
56-
return !warning.filename.includes('node_modules');
48+
return !warning.filename?.includes('node_modules');
5749
},
5850
}),
5951
],
6052
});
53+
54+
const file = Bun.file('meta.txt');
55+
await Bun.write(file, JSON.stringify(build.metafile, null, '\t'));
56+
57+
process.exit(0);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import esbuild from 'esbuild';
2+
import copy from 'esbuild-plugin-copy-watch';
3+
import esbuildSvelte from 'esbuild-svelte';
4+
import sveltePreprocess from 'svelte-preprocess';
5+
import manifest from '../../manifest.json' assert { type: 'json' };
6+
import { getBuildBanner } from 'build/buildBanner';
7+
8+
const banner = getBuildBanner('Dev Build', _ => 'Dev Build');
9+
10+
const context = await esbuild.context({
11+
banner: {
12+
js: banner,
13+
},
14+
entryPoints: ['src/main.ts'],
15+
bundle: true,
16+
external: [
17+
'obsidian',
18+
'electron',
19+
'@codemirror/autocomplete',
20+
'@codemirror/collab',
21+
'@codemirror/commands',
22+
'@codemirror/language',
23+
'@codemirror/lint',
24+
'@codemirror/search',
25+
'@codemirror/state',
26+
'@codemirror/view',
27+
'@lezer/common',
28+
'@lezer/highlight',
29+
'@lezer/lr',
30+
],
31+
format: 'cjs',
32+
target: 'es2018',
33+
logLevel: 'info',
34+
sourcemap: 'inline',
35+
treeShaking: true,
36+
outdir: `exampleVault/.obsidian/plugins/${manifest.id}/`,
37+
outbase: 'src',
38+
define: {
39+
MB_GLOBAL_CONFIG_DEV_BUILD: 'true',
40+
},
41+
plugins: [
42+
copy({
43+
paths: [
44+
{
45+
from: './styles.css',
46+
to: '',
47+
},
48+
{
49+
from: './manifest.json',
50+
to: '',
51+
},
52+
],
53+
}),
54+
esbuildSvelte({
55+
compilerOptions: { css: 'injected', dev: true, sveltePath: 'svelte' },
56+
preprocess: sveltePreprocess(),
57+
filterWarnings: warning => {
58+
// we don't want warnings from node modules that we can do nothing about
59+
return !warning.filename?.includes('node_modules');
60+
},
61+
}),
62+
],
63+
});
64+
65+
await context.watch();

automation/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devBranch": "master",
3+
"releaseBranch": "release",
4+
"github": "https://github.com/mProjectsCode/obsidian-media-db-plugin"
5+
}

automation/release.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { UserError } from 'utils/utils';
2+
import { CanaryVersion, Version, getIncrementOptions, parseVersion, stringifyVersion } from 'utils/versionUtils';
3+
import config from './config.json';
4+
import { $choice as $choice, $confirm, $seq, CMD_FMT, Verboseness } from 'utils/shellUtils';
5+
6+
async function runPreconditions(): Promise<void> {
7+
// run preconditions
8+
await $seq(
9+
[`bun run format`, `bun run lint:fix`, `bun run test`],
10+
(cmd: string) => {
11+
throw new UserError(`precondition "${cmd}" failed`);
12+
},
13+
() => {},
14+
undefined,
15+
Verboseness.VERBOSE,
16+
);
17+
18+
// add changed files
19+
await $seq(
20+
[`git add .`],
21+
() => {
22+
throw new UserError('failed to add preconditions changes to git');
23+
},
24+
() => {},
25+
undefined,
26+
Verboseness.NORMAL,
27+
);
28+
29+
// check if there were any changes
30+
let changesToCommit = false;
31+
await $seq(
32+
[`git diff --quiet`, `git diff --cached --quiet`],
33+
() => {
34+
changesToCommit = true;
35+
},
36+
() => {},
37+
undefined,
38+
Verboseness.QUITET,
39+
);
40+
41+
// if there were any changes, commit them
42+
if (changesToCommit) {
43+
await $seq(
44+
[`git commit -m "[auto] run release preconditions"`],
45+
() => {
46+
throw new UserError('failed to add preconditions changes to git');
47+
},
48+
() => {},
49+
undefined,
50+
Verboseness.NORMAL,
51+
);
52+
}
53+
}
54+
55+
async function run() {
56+
console.log('looking for untracked changes ...');
57+
58+
// check for any uncommited files and exit if there are any
59+
await $seq(
60+
[`git add .`, `git diff --quiet`, `git diff --cached --quiet`, `git checkout ${config.devBranch}`],
61+
() => {
62+
throw new UserError('there are still untracked changes');
63+
},
64+
() => {},
65+
undefined,
66+
Verboseness.QUITET,
67+
);
68+
69+
console.log('\nrunning preconditions ...\n');
70+
71+
await runPreconditions();
72+
73+
console.log('\nbumping versions ...\n');
74+
75+
const manifestFile = Bun.file('./manifest.json');
76+
const manifest = await manifestFile.json();
77+
78+
const versionString: string = manifest.version;
79+
const currentVersion: Version = parseVersion(versionString);
80+
const currentVersionString = stringifyVersion(currentVersion);
81+
82+
const versionIncrementOptions = getIncrementOptions(currentVersion);
83+
84+
const selectedIndex = await $choice(
85+
`Current version "${currentVersionString}". Select new version`,
86+
versionIncrementOptions.map(x => stringifyVersion(x)),
87+
);
88+
const newVersion = versionIncrementOptions[selectedIndex];
89+
const newVersionString = stringifyVersion(newVersion);
90+
91+
console.log('');
92+
93+
await $confirm(`Version will be updated "${currentVersionString}" -> "${newVersionString}". Are you sure`, () => {
94+
throw new UserError('user canceled script');
95+
});
96+
97+
if (!(newVersion instanceof CanaryVersion)) {
98+
manifest.version = newVersionString;
99+
}
100+
101+
await Bun.write(manifestFile, JSON.stringify(manifest, null, '\t'));
102+
103+
const betaManifest = structuredClone(manifest);
104+
betaManifest.version = newVersionString;
105+
106+
const betaManifestFile = Bun.file('./manifest-beta.json');
107+
await Bun.write(betaManifestFile, JSON.stringify(betaManifest, null, '\t'));
108+
109+
if (!(newVersion instanceof CanaryVersion)) {
110+
const versionsFile = Bun.file('./versions.json');
111+
const versionsJson = await versionsFile.json();
112+
113+
versionsJson[newVersionString] = manifest.minAppVersion;
114+
115+
await Bun.write(versionsFile, JSON.stringify(versionsJson, null, '\t'));
116+
117+
const packageFile = Bun.file('./package.json');
118+
const packageJson = await packageFile.json();
119+
120+
packageJson.version = newVersionString;
121+
122+
await Bun.write(packageFile, JSON.stringify(packageJson, null, '\t'));
123+
}
124+
125+
await $seq(
126+
[`bun run format`, `git add .`, `git commit -m "[auto] bump version to \`${newVersionString}\`"`],
127+
() => {
128+
throw new UserError('failed to add preconditions changes to git');
129+
},
130+
() => {},
131+
undefined,
132+
Verboseness.NORMAL,
133+
);
134+
135+
console.log('\ncreating release tag ...\n');
136+
137+
await $seq(
138+
[
139+
`git checkout ${config.releaseBranch}`,
140+
`git merge ${config.devBranch} --commit -m "[auto] merge \`${newVersionString}\` release commit"`,
141+
`git push origin ${config.releaseBranch}`,
142+
`git tag -a ${newVersionString} -m "release version ${newVersionString}"`,
143+
`git push origin ${newVersionString}`,
144+
`git checkout ${config.devBranch}`,
145+
`git merge ${config.releaseBranch}`,
146+
`git push origin ${config.devBranch}`,
147+
],
148+
() => {
149+
throw new UserError('failed to merge or create tag');
150+
},
151+
() => {},
152+
undefined,
153+
Verboseness.NORMAL,
154+
);
155+
156+
console.log('');
157+
158+
console.log(`${CMD_FMT.BgGreen}done${CMD_FMT.Reset}`);
159+
console.log(`${config.github}`);
160+
console.log(`${config.github}/releases/tag/${newVersionString}`);
161+
}
162+
163+
try {
164+
await run();
165+
} catch (e) {
166+
if (e instanceof UserError) {
167+
console.error(e.message);
168+
} else {
169+
console.error(e);
170+
}
171+
}

0 commit comments

Comments
 (0)