Skip to content

Commit

Permalink
Bundle loot-core types into the API
Browse files Browse the repository at this point in the history
So we can have loot-core be the source of truth
for some types that get passed through

- Improves downstream development with API by including types
- Use path aliases for dist vs dev tsconfigs
- Convert api index to typescript as example
- Permit ts-ignore for issues with our version of typescript
  • Loading branch information
twk3 committed Dec 9, 2023
1 parent 7955fe7 commit 206d4ee
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 19 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages/api/app/bundle.api.js
packages/api/dist
packages/api/@types
packages/api/migrations

packages/crdt/dist
Expand Down
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ module.exports = {
{ patterns: [...restrictedImportPatterns, ...restrictedImportColors] },
],

'@typescript-eslint/ban-ts-comment': [
'error',
{ 'ts-ignore': 'allow-with-description' },
],

// Rules disable during TS migration
'@typescript-eslint/no-var-requires': 'off',
'prefer-const': 'off',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
!data/.gitkeep
/data2
packages/api/dist
packages/api/@types
packages/crdt/dist
packages/desktop-electron/client-build
packages/desktop-electron/.electron-symbols
Expand Down
17 changes: 14 additions & 3 deletions packages/api/index.js → packages/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// @ts-ignore: false-positive commonjs module error on build until typescript 5.3
import type { RequestInfo as FetchRequestInfo } from 'node-fetch'; // with { 'resolution-mode': 'import' };

// loot-core types
import type { initConfig } from 'loot-core/server/main';

// eslint-disable-next-line import/extensions
import * as bundle from './app/bundle.api.js';

Check failure on line 8 in packages/api/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module './app/bundle.api.js' or its corresponding type declarations.
import * as injected from './injected';
Expand All @@ -12,15 +18,20 @@ export * as methods from './methods';
export * from './methods';
export * as utils from './utils';

export async function init(config = {}) {
export async function init(config: initConfig = {}) {
if (actualApp) {
return;
}

validateNodeVersion();

global.fetch = (...args) =>
import('node-fetch').then(({ default: fetch }) => fetch(...args));
if (!globalThis.fetch) {
globalThis.fetch = (url: URL | RequestInfo, init?: unknown) => {
return import('node-fetch').then(({ default: fetch }) =>
fetch(url as unknown as FetchRequestInfo, init),
) as unknown as Promise<Response>;
};
}

await bundle.init(config);
actualApp = bundle.lib;
Expand Down
13 changes: 8 additions & 5 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
"node": ">=18.12.0"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"types": "@types/index.d.ts",
"files": [
"dist"
"dist",
"@types/**/*.d.ts"
],
"scripts": {
"build:app": "yarn workspace loot-core build:api",
"build:node": "tsc --p tsconfig.dist.json",
"build:node": "tsc --p tsconfig.dist.json && tsc-alias -p tsconfig.dist.json",
"build:migrations": "cp migrations/*.sql dist/migrations",
"build:default-db": "cp default-db.sqlite dist/",
"build": "rm -rf dist && yarn run build:app && yarn run build:node && yarn run build:migrations && yarn run build:default-db",
"test": "yarn run build:app && jest -c jest.config.js"
"build": "yarn run clean && yarn run build:app && yarn run build:node && yarn run build:migrations && yarn run build:default-db",
"test": "yarn run build:app && jest -c jest.config.js",
"clean": "rm -rf dist @types"
},
"dependencies": {
"better-sqlite3": "^9.1.1",
Expand All @@ -31,6 +33,7 @@
"@types/jest": "^27.5.0",
"@types/uuid": "^9.0.2",
"jest": "^27.0.0",
"tsc-alias": "^1.8.8",
"typescript": "^5.0.2"
}
}
10 changes: 7 additions & 3 deletions packages/api/tsconfig.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
"moduleResolution": "Node16",
"noEmit": false,
"declaration": true,
"outDir": "dist"
"outDir": "dist",
"declarationDir": "@types",
"paths": {
"loot-core/*": ["./@types/loot-core/*"],
}
},
"include": ["."],
"exclude": ["dist"]
}
"exclude": ["dist", "@types"]
}
2 changes: 1 addition & 1 deletion packages/loot-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build:node": "cross-env NODE_ENV=production webpack --config ./webpack/webpack.desktop.config.js",
"watch:node": "cross-env NODE_ENV=development webpack --config ./webpack/webpack.desktop.config.js --watch",
"build:api": "cross-env NODE_ENV=development webpack --config ./webpack/webpack.api.config.js; ./bin/copy-migrations ../api",
"build:api": "cross-env NODE_ENV=development tsc -p tsconfig.api.json --outDir ../api/@types/loot-core/; webpack --config ./webpack/webpack.api.config.js; ./bin/copy-migrations ../api",
"build:browser": "cross-env NODE_ENV=production ./bin/build-browser",
"watch:browser": "cross-env NODE_ENV=development ./bin/build-browser",
"test": "npm-run-all -cp 'test:*'",
Expand Down
13 changes: 8 additions & 5 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2170,7 +2170,6 @@ async function setupDocumentsDir() {
fs._setDocumentDir(documentDir);
}

// eslint-disable-next-line import/no-unused-modules
export async function initApp(isDev, socketName) {

Check warning on line 2173 in packages/loot-core/src/server/main.ts

View workflow job for this annotation

GitHub Actions / lint

exported declaration 'initApp' not used within other modules
await sqlite.init();
await Promise.all([asyncStorage.init(), fs.init()]);
Expand Down Expand Up @@ -2233,8 +2232,13 @@ export async function initApp(isDev, socketName) {
}
}

// eslint-disable-next-line import/no-unused-modules
export async function init(config) {
export type initConfig = {
dataDir?: string;
serverURL?: string;
password?: string;
};

export async function init(config: initConfig) {

Check warning on line 2241 in packages/loot-core/src/server/main.ts

View workflow job for this annotation

GitHub Actions / lint

exported declaration 'init' not used within other modules
// Get from build

let dataDir, serverURL;
Expand Down Expand Up @@ -2272,11 +2276,10 @@ export async function init(config) {
}

// Export a few things required for the platform
// eslint-disable-next-line import/no-unused-modules
export const lib = {

Check warning on line 2279 in packages/loot-core/src/server/main.ts

View workflow job for this annotation

GitHub Actions / lint

exported declaration 'lib' not used within other modules
getDataDir: fs.getDataDir,
sendMessage: (msg, args) => connection.send(msg, args),
send: async (name, args) => {
send: async (name, args?) => {
let res = await runHandler(app.handlers[name], args);
return res;
},
Expand Down
12 changes: 12 additions & 0 deletions packages/loot-core/tsconfig.api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"declaration": true,
"emitDeclarationOnly": true,
"allowJs": false,
"noEmit": false,
},
"include": ["./typings", "./src/server/*"],
"exclude": ["**/node_modules/*", "**/build/*", "**/lib-dist/*", "./src/server/bench.ts"],
}
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
"moduleResolution": "Node",
"module": "ES2022",
// Until/if we build using tsc
"noEmit": true
"noEmit": true,
"paths": {
// until we turn on composite/references
"loot-core/*": ["./packages/loot-core/src/*"],
},
},
"include": ["packages/**/*"],
"exclude": ["**/node_modules/*", "**/build/*", "**/lib-dist/*"],
Expand Down
42 changes: 41 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ __metadata:
compare-versions: "npm:^6.1.0"
jest: "npm:^27.0.0"
node-fetch: "npm:^3.3.2"
tsc-alias: "npm:^1.8.8"
typescript: "npm:^5.0.2"
uuid: "npm:^9.0.0"
languageName: unknown
Expand Down Expand Up @@ -7139,7 +7140,7 @@ __metadata:
languageName: node
linkType: hard

"commander@npm:^9.4.1":
"commander@npm:^9.0.0, commander@npm:^9.4.1":
version: 9.5.0
resolution: "commander@npm:9.5.0"
checksum: 41c49b3d0f94a1fbeb0463c85b13f15aa15a9e0b4d5e10a49c0a1d58d4489b549d62262b052ae0aa6cfda53299bee487bfe337825df15e342114dde543f82906
Expand Down Expand Up @@ -14575,6 +14576,13 @@ __metadata:
languageName: node
linkType: hard

"mylas@npm:^2.1.9":
version: 2.1.13
resolution: "mylas@npm:2.1.13"
checksum: 37f335424463c422f48d50317aa0a34fe410fabb146cbf27b453a0aa743732b5626f56deaa190bca2ce29836f809d88759007976dc78d5d22b75918a00586577
languageName: node
linkType: hard

"mz@npm:^2.7.0":
version: 2.7.0
resolution: "mz@npm:2.7.0"
Expand Down Expand Up @@ -15663,6 +15671,15 @@ __metadata:
languageName: node
linkType: hard

"plimit-lit@npm:^1.2.6":
version: 1.6.1
resolution: "plimit-lit@npm:1.6.1"
dependencies:
queue-lit: "npm:^1.5.1"
checksum: e4eaf018dc311fd4d452954c10992cd8a9eb72d168ec2274bb831d86558422703e1405a8978ffdd5c418654e6a25e10a0765a39bf3ce3a84dc799fe6268e0ea4
languageName: node
linkType: hard

"plist@npm:^3.0.4":
version: 3.0.6
resolution: "plist@npm:3.0.6"
Expand Down Expand Up @@ -16815,6 +16832,13 @@ __metadata:
languageName: node
linkType: hard

"queue-lit@npm:^1.5.1":
version: 1.5.2
resolution: "queue-lit@npm:1.5.2"
checksum: 8dd45c79bd25b33b0c7d587391eb0b4acc4deb797bf92fef62b2d8e7c03b64083f5304f09d52a18267d34d020cc67ccde97a88185b67590eeccb194938ff1f98
languageName: node
linkType: hard

"queue-microtask@npm:^1.2.2":
version: 1.2.3
resolution: "queue-microtask@npm:1.2.3"
Expand Down Expand Up @@ -19680,6 +19704,22 @@ __metadata:
languageName: node
linkType: hard

"tsc-alias@npm:^1.8.8":
version: 1.8.8
resolution: "tsc-alias@npm:1.8.8"
dependencies:
chokidar: "npm:^3.5.3"
commander: "npm:^9.0.0"
globby: "npm:^11.0.4"
mylas: "npm:^2.1.9"
normalize-path: "npm:^3.0.0"
plimit-lit: "npm:^1.2.6"
bin:
tsc-alias: dist/bin/index.js
checksum: 145d7bb23a618e1136c8addd4b4ed23a1d503a37d3fc5b3698a993fea9331180a68853b0e78ff50fb3fb7ed95d4996a2d82f77395814bbd1c40adee8a9151d90
languageName: node
linkType: hard

"tsconfig-paths@npm:^3.14.1":
version: 3.14.2
resolution: "tsconfig-paths@npm:3.14.2"
Expand Down

0 comments on commit 206d4ee

Please sign in to comment.