-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
226 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
apps/joplin-vscode-plugin/src/util/__tests__/UploadResourceUtil.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { close, mkdirp, pathExists, readFile, remove } from '@liuli-util/fs-extra' | ||
import { config, resourceApi } from 'joplin-api' | ||
import path from 'path' | ||
import { beforeEach, expect, it } from 'vitest' | ||
import { findParent } from '../findParent' | ||
import { UploadResourceUtil } from '../UploadResourceUtil' | ||
import { parse } from 'envfile' | ||
import { createEmptyFile } from '../createEmptyFile' | ||
import '../node-polyfill' | ||
|
||
const tempPath = path.resolve(__dirname, '.temp', path.basename(__filename)) | ||
beforeEach(async () => { | ||
await remove(tempPath) | ||
await mkdirp(tempPath) | ||
const dirPath = await findParent(__dirname, (item) => pathExists(path.resolve(item, 'package.json'))) | ||
const envPath = path.resolve(dirPath!, '.env.local') | ||
if (!(await pathExists(envPath))) { | ||
throw new Error('请更新 .env.local 文件:' + envPath) | ||
} | ||
const env = await readFile(envPath, 'utf8') | ||
|
||
config.token = parse(env).TOKEN! | ||
config.baseUrl = 'http://127.0.0.1:27583' | ||
}) | ||
|
||
it('test create by empty file', async () => { | ||
const fsPath = path.resolve(__dirname, 'assets/test.km.svg') | ||
const { res, markdownLink } = await UploadResourceUtil.uploadByPath(fsPath, true) | ||
expect(res.mime).eq('image/svg+xml') | ||
expect(markdownLink).eq(`![${path.basename(fsPath)}](:/${res.id})`) | ||
const data = await readFile(fsPath) | ||
const buffer = await resourceApi.fileByResourceId(res.id) | ||
expect(data).deep.eq(buffer) | ||
console.log(data.length) | ||
}) |
1 change: 1 addition & 0 deletions
1
apps/joplin-vscode-plugin/src/util/__tests__/assets/test.km.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import path from 'path' | ||
|
||
/** | ||
* 向上查找目录 | ||
* @param cwd | ||
* @param predicate | ||
*/ | ||
export function findParent(cwd: string, predicate: (dir: string) => boolean): string | null | ||
export function findParent(cwd: string, predicate: (dir: string) => Promise<boolean>): Promise<string | null> | ||
export function findParent<T extends (dir: string) => boolean | Promise<boolean>, R extends string | null>( | ||
cwd: string, | ||
predicate: T, | ||
): ReturnType<T> extends Promise<any> ? Promise<R> : R { | ||
const res = predicate(cwd) | ||
function f(res: boolean): string | null { | ||
if (res) { | ||
return cwd | ||
} | ||
const parent = path.dirname(cwd) | ||
if (parent === cwd) { | ||
return null | ||
} | ||
return findParent(parent, predicate as any) | ||
} | ||
|
||
return res instanceof Promise ? res.then(f) : (f(res) as any) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import nodeFetch from 'node-fetch' | ||
import fetch from 'node-fetch' | ||
import { FormData } from 'formdata-polyfill/esm.min.js' | ||
import { Blob } from 'fetch-blob' | ||
|
||
// @ts-expect-errors | ||
if (typeof fetch === 'undefined') { | ||
Reflect.set(globalThis, 'fetch', nodeFetch) | ||
} | ||
if (typeof FormData === 'undefined') { | ||
Reflect.set(globalThis, 'FormData', FormData) | ||
} | ||
Reflect.set(globalThis, 'fetch', fetch) | ||
Reflect.set(globalThis, 'FormData', FormData) | ||
Reflect.set(globalThis, 'Blob', Blob) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions
61
libs/joplin-api/src/api/__tests__/fixs/ResourceApiCreateByPolyfill.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { mkdirp, pathExists, readFile, remove, open, close, writeFile } from 'fs-extra' | ||
import path from 'path' | ||
import { beforeEach, expect, it } from 'vitest' | ||
import { resourceApi } from '../../JoplinApiGenerator' | ||
import fetch from 'node-fetch' | ||
import { FormData } from 'formdata-polyfill/esm.min.js' | ||
import { Blob } from 'fetch-blob' | ||
|
||
const tempPath = path.resolve(__dirname, '.temp') | ||
beforeEach(async () => { | ||
await remove(tempPath) | ||
await mkdirp(tempPath) | ||
Reflect.set(globalThis, 'fetch', fetch) | ||
Reflect.set(globalThis, 'FormData', FormData) | ||
Reflect.set(globalThis, 'Blob', Blob) | ||
}) | ||
|
||
it('create empty file and polyfill', async () => { | ||
const emptyPath = path.resolve(tempPath, 'test.km.svg') | ||
const handle = await open(emptyPath, 'w') | ||
try { | ||
expect(await pathExists(emptyPath)).toBeTruthy() | ||
const r = await resourceApi.create({ | ||
title: path.basename(emptyPath), | ||
data: new Blob([await readFile(emptyPath)]), | ||
}) | ||
expect(r).not.undefined | ||
} finally { | ||
await close(handle) | ||
} | ||
}) | ||
|
||
it('create file and polyfill', async () => { | ||
const fsPath = path.resolve(__dirname, '../assets/resourcesByFileId.png') | ||
const data = await readFile(fsPath) | ||
const r = await resourceApi.create({ | ||
title: path.basename(fsPath), | ||
data: new Blob([data]), | ||
}) | ||
expect(r).not.undefined | ||
expect(data).deep.eq(await resourceApi.fileByResourceId(r.id)) | ||
}) | ||
|
||
it('update file', async () => { | ||
const emptyPath = path.resolve(tempPath, 'test.km.svg') | ||
const fsPath = path.resolve(__dirname, '../assets/resourcesByFileId.png') | ||
const handle = await open(emptyPath, 'w') | ||
try { | ||
expect(await pathExists(emptyPath)).toBeTruthy() | ||
const r = await resourceApi.create({ | ||
title: path.basename(emptyPath), | ||
data: new Blob([await readFile(emptyPath)]), | ||
}) | ||
expect(r).not.undefined | ||
const data = await readFile(fsPath) | ||
await resourceApi.update({ id: r.id, data: new Blob([data]) }) | ||
expect(data).deep.eq(await resourceApi.fileByResourceId(r.id)) | ||
} finally { | ||
await close(handle) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.