Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettier #135

Merged
merged 4 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Workflow for code checks
name: Check code

on:
pull_request:
branches: [master]
push:
branches: [master]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 20.x
- run: yarn install
- run: yarn run lint:check:prettier
- run: yarn run lint:check:types
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore artifacts:
build
dist
node_modules
packaged
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"deno.enable": false,
"typescript.tsdk": "node_modules/typescript/lib",
}
"typescript.tsdk": "node_modules/typescript/lib"
}
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Chronicles
Electron based markdown journaling application, in the spirit of [incremental note taking][incr-notes].

**Status**: Hobby project, prototyping and re-working UX to try out various concepts with little regard for usability, stability, or appearances. Will remove this clause when I can guarantee your notes won't *poof* and the experience isn't confusing. Reach out if you are interested in journaling concepts and I'll provide a tour.
Electron based markdown journaling application, in the spirit of [incremental note taking][incr-notes].

**Status**: Hobby project, prototyping and re-working UX to try out various concepts with little regard for usability, stability, or appearances. Will remove this clause when I can guarantee your notes won't _poof_ and the experience isn't confusing. Reach out if you are interested in journaling concepts and I'll provide a tour.

Tech stack:

- Electron and esbuild
- Typescript
- React and mobx
- Slate and Plate (Notion style WSYIWYG)


## Development
The app is a typical Electron dev setup, excepting the use of [esbuild][1].

The app is a typical Electron dev setup, excepting the use of [esbuild][1].

```bash
# install dependencies
Expand All @@ -24,20 +26,21 @@ yarn start
yarn run electron-rebuild
```

See scripts/dev.js for specifics on how the source files are compiled and re-loaded in development.
See scripts/dev.js for specifics on how the source files are compiled and re-loaded in development.

## Build and release

- Read and use the `build.sh` script
- Make a Github release

At a high level, the build is comprised of:

- generate bundles ([esbuild][1]) from source files
- install production dependencies
- [re-build][2] native dependencies for the targeted electron version
- [package][3] the app


[1]: https://esbuild.github.io
[2]: https://github.com/electron/electron-rebuild
[3]: https://github.com/electron/electron-packager
[incr-notes]: https://thesephist.com/posts/inc/
[incr-notes]: https://thesephist.com/posts/inc/
55 changes: 28 additions & 27 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
/**
* See https://github.com/electron/electron-rebuild
* and https://www.electronjs.org/docs/tutorial/using-native-node-modules
*
*
* This file was created specifically to handle re-building sqlite3
* That is, sqlite3 is a _native_ dependency, and where there is a mis-match
* between the development nodejs version, and the target electron version of nodejs (v8?),
* native dependencies need to be re-compiled for the target platform.
*
* This is required anytime the app is built and packaged for distribution, so it
* should be run as part of the build script.
*
* The build.sh script could handle this functionality, but it would need to
*
* This is required anytime the app is built and packaged for distribution, so it
* should be run as part of the build script.
*
* The build.sh script could handle this functionality, but it would need to
* run after node_modules are installed IN the packaged directory
*
*
* This is also true for any additional architecture beyond whatever macos supports.
*/
const packager = require('electron-packager');
const { rebuild } = require('@electron/rebuild');
const packager = require("electron-packager");
const { rebuild } = require("@electron/rebuild");

// These arguments are provided by build.sh
const srcDir = process.argv[2];
const outDir = process.argv[3];

if (!srcDir || !outDir) {
console.error('srcDir and outDir are required arguments to package.js');
console.error("srcDir and outDir are required arguments to package.js");
process.exit(1);
}

// sanity
console.log(
'Electron packager and rebuild, using source directory',
"Electron packager and rebuild, using source directory",
srcDir,
'and outputting to',
outDir
"and outputting to",
outDir,
);


packager({
dir: srcDir,
out: outDir,
// … other options
// Documentation does this in afterCopy. Why did I do this in afterPrune?
afterPrune: [(buildPath, electronVersion, platform, arch, callback) => {
console.log('rebuilding...', buildPath, electronVersion, platform, arch);

// Previously, and after they are fixed:
rebuild({ buildPath, electronVersion, arch })
.then(() => callback())
.catch((error) => {
console.error('Error rebuilding native dependencies!');
console.error(error);
callback(error)
});
}],
});
afterPrune: [
(buildPath, electronVersion, platform, arch, callback) => {
console.log("rebuilding...", buildPath, electronVersion, platform, arch);

// Previously, and after they are fixed:
rebuild({ buildPath, electronVersion, arch })
.then(() => callback())
.catch((error) => {
console.error("Error rebuilding native dependencies!");
console.error(error);
callback(error);
});
},
],
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"main": "main.bundle.js",
"license": "UNLICENSED",
"scripts": {
"lint:check:prettier": "prettier . --check",
"lint:check:types": "tsc --noEmit --skipLibCheck",
"start": "node ./scripts/dev.js",
"test": "mocha -r esm -r ts-node/register src/**/*.test.ts",
"test:one": "mocha -r ts-node/register -r esm"
Expand Down Expand Up @@ -44,6 +46,7 @@
"mobx": "^5.15.4",
"mobx-react-lite": "^2.0.7",
"mocha": "^9.1.3",
"prettier": "3.2.4",
"react": "^17.0.2",
"react-day-picker": "^8.0.2",
"react-dom": "^17.0.2",
Expand Down
114 changes: 62 additions & 52 deletions scripts/dev.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const esbuild = require('esbuild');
const cp = require('child_process');
const electron = require('electron')
const lodash = require('lodash')
const esbuild = require("esbuild");
const cp = require("child_process");
const electron = require("electron");
const lodash = require("lodash");

// After successful rebuild, log results
function afterRebuild(name, cb) {
Expand All @@ -10,9 +10,9 @@ function afterRebuild(name, cb) {
console.error(`${name} bundle completed with error`, error);
} else {
console.log(`${name} bundle completed`);
restartElectron()
restartElectron();
}
}
};
}

// After successful build, log results
Expand All @@ -27,44 +27,50 @@ function afterBuild(name) {
}

// note: until I see errors or warnings, unsure if I should have any special behavior...
startElectron()
}
startElectron();
};
}

// build renderer bundle
esbuild.build({
entryPoints: ['src/index.tsx'],
outfile: 'src/renderer.bundle.js',
bundle: true,
platform: 'browser',
watch: {
onRebuild: afterRebuild('renderer')
},
}).then(afterBuild('renderer'), console.error);
esbuild
.build({
entryPoints: ["src/index.tsx"],
outfile: "src/renderer.bundle.js",
bundle: true,
platform: "browser",
watch: {
onRebuild: afterRebuild("renderer"),
},
})
.then(afterBuild("renderer"), console.error);

// build preload bundle
esbuild.build({
entryPoints: ['src/preload/index.ts'],
outfile: 'src/preload.bundle.js',
bundle: true,
platform: 'node',
external: ['knex', 'electron', 'electron-store', 'better-sqlite3'],
watch: {
onRebuild: afterRebuild('preload')
},
}).then(afterBuild('preload'), console.error);
esbuild
.build({
entryPoints: ["src/preload/index.ts"],
outfile: "src/preload.bundle.js",
bundle: true,
platform: "node",
external: ["knex", "electron", "electron-store", "better-sqlite3"],
watch: {
onRebuild: afterRebuild("preload"),
},
})
.then(afterBuild("preload"), console.error);

// build electron main bundle
esbuild.build({
entryPoints: ['src/electron/index.js'],
outfile: 'src/main.bundle.js',
bundle: true,
platform: 'node',
external: ['electron', 'electron-store', 'better-sqlite3'],
watch: {
onRebuild: afterRebuild('main')
},
}).then(afterBuild('main'), console.error);
esbuild
.build({
entryPoints: ["src/electron/index.js"],
outfile: "src/main.bundle.js",
bundle: true,
platform: "node",
external: ["electron", "electron-store", "better-sqlite3"],
watch: {
onRebuild: afterRebuild("main"),
},
})
.then(afterBuild("main"), console.error);

// For holding the spawned Electron main process, so it can
// be .kill()'ed on re-start
Expand All @@ -77,27 +83,31 @@ let startCounter = 0;
function startElectron() {
// Naive way to wait for all three bundles before starting the first time, since
// main bundle completes quickest
startCounter++
startCounter++;
if (startCounter < 3) return;

console.log('starting electron')
eprocess = cp.spawn(`${electron}`, ['src/main.bundle.js'], { stdio: 'inherit' });
console.log("starting electron");
eprocess = cp.spawn(`${electron}`, ["src/main.bundle.js"], {
stdio: "inherit",
});

eprocess.on('error', (error) => {
console.error('electron error', error, error.message)
process.exit(1)
})
eprocess.on("error", (error) => {
console.error("electron error", error, error.message);
process.exit(1);
});
}

// Re-start the main process
const restartElectron = lodash.debounce(function startElectron() {
if (eprocess) eprocess.kill('SIGTERM');
if (eprocess) eprocess.kill("SIGTERM");

console.log('starting electron')
eprocess = cp.spawn(`${electron}`, ['src/main.bundle.js'], { stdio: 'inherit' });
console.log("starting electron");
eprocess = cp.spawn(`${electron}`, ["src/main.bundle.js"], {
stdio: "inherit",
});

eprocess.on('error', (error) => {
console.error('electron error', error, error.message)
process.exit(1)
})
}, 200)
eprocess.on("error", (error) => {
console.error("electron error", error, error.message);
process.exit(1);
});
}, 200);
20 changes: 11 additions & 9 deletions scripts/migrate-uuidv7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
// against a target electron version, rather than the local node version. So
// I run it in the preload/client/index.ts file; keeping here for reference.
import { V7Generator } from "uuidv7";
import Knex from 'knex';
import Knex from "knex";

const knex = Knex({
client: 'better-sqlite3',
client: "better-sqlite3",
connection: {
filename: '/Users/my/sqlite3database.db'
}
filename: "/Users/my/sqlite3database.db",
},
});

function uuidV7FromTimestamp(ts: string) {
Expand All @@ -23,10 +23,12 @@ function uuidV7FromTimestamp(ts: string) {
}

async function validateSortOrder() {
const journals = await knex('journals');
const journals = await knex("journals");

// All documents sorted by date; sort is for fudging the timestamp (see below)
const documents = await knex('documents').select('id', 'title', 'createdAt', 'journalId').orderBy('createdAt', 'desc');
const documents = await knex("documents")
.select("id", "title", "createdAt", "journalId")
.orderBy("createdAt", "desc");

// Adding a UUIDv7 based on createdAt to each document
documents.forEach((doc, idx) => {
Expand All @@ -43,14 +45,14 @@ async function validateSortOrder() {
// For now only updating id, not createdAt, b/c I can't definitely say
// I want to discard the old createdAt value. This is only relevant for
// my personal documents anyways.
await knex('documents').where({ id: doc.id }).update({ id: doc.uuid })
await knex("documents").where({ id: doc.id }).update({ id: doc.uuid });
}

// now update journals
for (const j of journals) {
const uuid = uuidV7FromTimestamp(j.createdAt);
await knex('journals').where({ id: j.id }).update({ id: uuid })
await knex("journals").where({ id: j.id }).update({ id: uuid });
}
}

validateSortOrder()
validateSortOrder();
Loading