refactor(web): dependency management (#2386)
This commit is contained in:
@@ -1,19 +1,13 @@
|
||||
import { WebApplicationInterface } from './../../WebApplication/WebApplicationInterface'
|
||||
import { NativeFeatureIdentifier, NoteType } from '@standardnotes/features'
|
||||
import { AegisToAuthenticatorConverter } from './AegisToAuthenticatorConverter'
|
||||
import data from './testData'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
|
||||
UuidGenerator.SetGenerator(() => String(Math.random()))
|
||||
|
||||
describe('AegisConverter', () => {
|
||||
let application: WebApplicationInterface
|
||||
|
||||
beforeEach(() => {
|
||||
application = {
|
||||
generateUUID: jest.fn().mockReturnValue('test'),
|
||||
} as unknown as WebApplicationInterface
|
||||
})
|
||||
|
||||
it('should parse entries', () => {
|
||||
const converter = new AegisToAuthenticatorConverter(application)
|
||||
const converter = new AegisToAuthenticatorConverter()
|
||||
|
||||
const result = converter.parseEntries(data)
|
||||
|
||||
@@ -34,7 +28,7 @@ describe('AegisConverter', () => {
|
||||
})
|
||||
|
||||
it('should create note from entries with editor info', () => {
|
||||
const converter = new AegisToAuthenticatorConverter(application)
|
||||
const converter = new AegisToAuthenticatorConverter()
|
||||
|
||||
const parsedEntries = converter.parseEntries(data)
|
||||
|
||||
@@ -61,7 +55,7 @@ describe('AegisConverter', () => {
|
||||
})
|
||||
|
||||
it('should create note from entries without editor info', () => {
|
||||
const converter = new AegisToAuthenticatorConverter(application)
|
||||
const converter = new AegisToAuthenticatorConverter()
|
||||
|
||||
const parsedEntries = converter.parseEntries(data)
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { DecryptedTransferPayload, NoteContent } from '@standardnotes/models'
|
||||
import { readFileAsText } from '../Utils'
|
||||
import { NativeFeatureIdentifier, NoteType } from '@standardnotes/features'
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
|
||||
type AegisData = {
|
||||
db: {
|
||||
@@ -27,9 +27,11 @@ type AuthenticatorEntry = {
|
||||
}
|
||||
|
||||
export class AegisToAuthenticatorConverter {
|
||||
constructor(protected application: WebApplicationInterface) {}
|
||||
constructor() {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
static isValidAegisJson(json: any): boolean {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return json.db && json.db.entries && json.db.entries.every((entry: any) => AegisEntryTypes.includes(entry.type))
|
||||
}
|
||||
|
||||
@@ -61,7 +63,7 @@ export class AegisToAuthenticatorConverter {
|
||||
created_at_timestamp: file.lastModified,
|
||||
updated_at: new Date(file.lastModified),
|
||||
updated_at_timestamp: file.lastModified,
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Note,
|
||||
content: {
|
||||
title: file.name.split('.')[0],
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ContentType } from '@standardnotes/domain-core'
|
||||
import { DecryptedTransferPayload, NoteContent, TagContent } from '@standardnotes/models'
|
||||
import { EvernoteConverter } from './EvernoteConverter'
|
||||
import data from './testData'
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
|
||||
// Mock dayjs so dayjs.extend() doesn't throw an error in EvernoteConverter.ts
|
||||
jest.mock('dayjs', () => {
|
||||
@@ -21,17 +21,11 @@ jest.mock('dayjs', () => {
|
||||
}
|
||||
})
|
||||
|
||||
UuidGenerator.SetGenerator(() => String(Math.random()))
|
||||
|
||||
describe('EvernoteConverter', () => {
|
||||
let application: WebApplicationInterface
|
||||
|
||||
beforeEach(() => {
|
||||
application = {
|
||||
generateUUID: jest.fn().mockReturnValue(Math.random()),
|
||||
} as any as WebApplicationInterface
|
||||
})
|
||||
|
||||
it('should parse and strip html', () => {
|
||||
const converter = new EvernoteConverter(application)
|
||||
const converter = new EvernoteConverter()
|
||||
|
||||
const result = converter.parseENEXData(data, true)
|
||||
|
||||
@@ -51,7 +45,7 @@ describe('EvernoteConverter', () => {
|
||||
})
|
||||
|
||||
it('should parse and not strip html', () => {
|
||||
const converter = new EvernoteConverter(application)
|
||||
const converter = new EvernoteConverter()
|
||||
|
||||
const result = converter.parseENEXData(data, false)
|
||||
|
||||
|
||||
@@ -3,15 +3,15 @@ import { readFileAsText } from '../Utils'
|
||||
import dayjs from 'dayjs'
|
||||
import customParseFormat from 'dayjs/plugin/customParseFormat'
|
||||
import utc from 'dayjs/plugin/utc'
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
dayjs.extend(customParseFormat)
|
||||
dayjs.extend(utc)
|
||||
|
||||
const dateFormat = 'YYYYMMDDTHHmmss'
|
||||
|
||||
export class EvernoteConverter {
|
||||
constructor(protected application: WebApplicationInterface) {}
|
||||
constructor() {}
|
||||
|
||||
async convertENEXFileToNotesAndTags(file: File, stripHTML: boolean): Promise<DecryptedTransferPayload[]> {
|
||||
const content = await readFileAsText(file)
|
||||
@@ -35,7 +35,7 @@ export class EvernoteConverter {
|
||||
created_at_timestamp: now.getTime(),
|
||||
updated_at: now,
|
||||
updated_at_timestamp: now.getTime(),
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Tag,
|
||||
content: {
|
||||
title: defaultTagName,
|
||||
@@ -88,7 +88,7 @@ export class EvernoteConverter {
|
||||
created_at_timestamp: createdAtDate.getTime(),
|
||||
updated_at: updatedAtDate,
|
||||
updated_at_timestamp: updatedAtDate.getTime(),
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Note,
|
||||
content: {
|
||||
title: !title ? `Imported note ${index + 1} from Evernote` : title,
|
||||
@@ -111,7 +111,7 @@ export class EvernoteConverter {
|
||||
if (!tag) {
|
||||
const now = new Date()
|
||||
tag = {
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Tag,
|
||||
created_at: now,
|
||||
created_at_timestamp: now.getTime(),
|
||||
|
||||
@@ -4,19 +4,13 @@
|
||||
|
||||
import { jsonTestData, htmlTestData } from './testData'
|
||||
import { GoogleKeepConverter } from './GoogleKeepConverter'
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
|
||||
UuidGenerator.SetGenerator(() => String(Math.random()))
|
||||
|
||||
describe('GoogleKeepConverter', () => {
|
||||
let application: WebApplicationInterface
|
||||
|
||||
beforeEach(() => {
|
||||
application = {
|
||||
generateUUID: jest.fn().mockReturnValue('uuid'),
|
||||
} as unknown as WebApplicationInterface
|
||||
})
|
||||
|
||||
it('should parse json data', () => {
|
||||
const converter = new GoogleKeepConverter(application)
|
||||
const converter = new GoogleKeepConverter()
|
||||
|
||||
const result = converter.tryParseAsJson(jsonTestData)
|
||||
|
||||
@@ -33,7 +27,7 @@ describe('GoogleKeepConverter', () => {
|
||||
})
|
||||
|
||||
it('should parse html data', () => {
|
||||
const converter = new GoogleKeepConverter(application)
|
||||
const converter = new GoogleKeepConverter()
|
||||
|
||||
const result = converter.tryParseAsHtml(
|
||||
htmlTestData,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
import { DecryptedTransferPayload, NoteContent } from '@standardnotes/models'
|
||||
import { readFileAsText } from '../Utils'
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
|
||||
type GoogleKeepJsonNote = {
|
||||
color: string
|
||||
@@ -14,7 +14,7 @@ type GoogleKeepJsonNote = {
|
||||
}
|
||||
|
||||
export class GoogleKeepConverter {
|
||||
constructor(protected application: WebApplicationInterface) {}
|
||||
constructor() {}
|
||||
|
||||
async convertGoogleKeepBackupFileToNote(
|
||||
file: File,
|
||||
@@ -66,7 +66,7 @@ export class GoogleKeepConverter {
|
||||
created_at_timestamp: date.getTime(),
|
||||
updated_at: date,
|
||||
updated_at_timestamp: date.getTime(),
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Note,
|
||||
content: {
|
||||
title: title,
|
||||
@@ -96,6 +96,7 @@ export class GoogleKeepConverter {
|
||||
return
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
static isValidGoogleKeepJson(json: any): boolean {
|
||||
return (
|
||||
typeof json.title === 'string' &&
|
||||
@@ -120,7 +121,7 @@ export class GoogleKeepConverter {
|
||||
created_at_timestamp: date.getTime(),
|
||||
updated_at: date,
|
||||
updated_at_timestamp: date.getTime(),
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Note,
|
||||
content: {
|
||||
title: parsed.title,
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { parseFileName } from '@standardnotes/filepicker'
|
||||
import { FeatureStatus } from '@standardnotes/services'
|
||||
import {
|
||||
FeatureStatus,
|
||||
FeaturesClientInterface,
|
||||
ItemManagerInterface,
|
||||
MutatorClientInterface,
|
||||
} from '@standardnotes/services'
|
||||
import { NativeFeatureIdentifier } from '@standardnotes/features'
|
||||
import { AegisToAuthenticatorConverter } from './AegisConverter/AegisToAuthenticatorConverter'
|
||||
import { EvernoteConverter } from './EvernoteConverter/EvernoteConverter'
|
||||
@@ -8,7 +13,6 @@ import { PlaintextConverter } from './PlaintextConverter/PlaintextConverter'
|
||||
import { SimplenoteConverter } from './SimplenoteConverter/SimplenoteConverter'
|
||||
import { readFileAsText } from './Utils'
|
||||
import { DecryptedTransferPayload, NoteContent } from '@standardnotes/models'
|
||||
import { WebApplicationInterface } from '../WebApplication/WebApplicationInterface'
|
||||
|
||||
export type NoteImportType = 'plaintext' | 'evernote' | 'google-keep' | 'simplenote' | 'aegis'
|
||||
|
||||
@@ -19,12 +23,16 @@ export class Importer {
|
||||
plaintextConverter: PlaintextConverter
|
||||
evernoteConverter: EvernoteConverter
|
||||
|
||||
constructor(protected application: WebApplicationInterface) {
|
||||
this.aegisConverter = new AegisToAuthenticatorConverter(application)
|
||||
this.googleKeepConverter = new GoogleKeepConverter(application)
|
||||
this.simplenoteConverter = new SimplenoteConverter(application)
|
||||
this.plaintextConverter = new PlaintextConverter(application)
|
||||
this.evernoteConverter = new EvernoteConverter(application)
|
||||
constructor(
|
||||
private features: FeaturesClientInterface,
|
||||
private mutator: MutatorClientInterface,
|
||||
private items: ItemManagerInterface,
|
||||
) {
|
||||
this.aegisConverter = new AegisToAuthenticatorConverter()
|
||||
this.googleKeepConverter = new GoogleKeepConverter()
|
||||
this.simplenoteConverter = new SimplenoteConverter()
|
||||
this.plaintextConverter = new PlaintextConverter()
|
||||
this.evernoteConverter = new EvernoteConverter()
|
||||
}
|
||||
|
||||
static detectService = async (file: File): Promise<NoteImportType | null> => {
|
||||
@@ -64,7 +72,7 @@ export class Importer {
|
||||
async getPayloadsFromFile(file: File, type: NoteImportType): Promise<DecryptedTransferPayload[]> {
|
||||
if (type === 'aegis') {
|
||||
const isEntitledToAuthenticator =
|
||||
this.application.features.getFeatureStatus(
|
||||
this.features.getFeatureStatus(
|
||||
NativeFeatureIdentifier.create(NativeFeatureIdentifier.TYPES.TokenVaultEditor).getValue(),
|
||||
) === FeatureStatus.Entitled
|
||||
return [await this.aegisConverter.convertAegisBackupFileToNote(file, isEntitledToAuthenticator)]
|
||||
@@ -85,7 +93,7 @@ export class Importer {
|
||||
const insertedItems = await Promise.all(
|
||||
payloads.map(async (payload) => {
|
||||
const content = payload.content as NoteContent
|
||||
const note = this.application.items.createTemplateItem(
|
||||
const note = this.items.createTemplateItem(
|
||||
payload.content_type,
|
||||
{
|
||||
text: content.text,
|
||||
@@ -100,7 +108,7 @@ export class Importer {
|
||||
uuid: payload.uuid,
|
||||
},
|
||||
)
|
||||
return this.application.mutator.insertItem(note)
|
||||
return this.mutator.insertItem(note)
|
||||
}),
|
||||
)
|
||||
return insertedItems
|
||||
|
||||
@@ -2,11 +2,9 @@ import { ContentType } from '@standardnotes/domain-core'
|
||||
import { parseFileName } from '@standardnotes/filepicker'
|
||||
import { DecryptedTransferPayload, NoteContent } from '@standardnotes/models'
|
||||
import { readFileAsText } from '../Utils'
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
|
||||
export class PlaintextConverter {
|
||||
constructor(protected application: WebApplicationInterface) {}
|
||||
|
||||
static isValidPlaintextFile(file: File): boolean {
|
||||
return file.type === 'text/plain' || file.type === 'text/markdown'
|
||||
}
|
||||
@@ -24,7 +22,7 @@ export class PlaintextConverter {
|
||||
created_at_timestamp: createdAtDate.getTime(),
|
||||
updated_at: updatedAtDate,
|
||||
updated_at_timestamp: updatedAtDate.getTime(),
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Note,
|
||||
content: {
|
||||
title: name,
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
import { SimplenoteConverter } from './SimplenoteConverter'
|
||||
import data from './testData'
|
||||
|
||||
UuidGenerator.SetGenerator(() => String(Math.random()))
|
||||
|
||||
describe('SimplenoteConverter', () => {
|
||||
let application: WebApplicationInterface
|
||||
|
||||
beforeEach(() => {
|
||||
application = {
|
||||
generateUUID: jest.fn().mockReturnValue('uuid'),
|
||||
} as any
|
||||
})
|
||||
|
||||
it('should parse', () => {
|
||||
const converter = new SimplenoteConverter(application)
|
||||
const converter = new SimplenoteConverter()
|
||||
|
||||
const result = converter.parse(data)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { DecryptedTransferPayload, NoteContent } from '@standardnotes/models'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
import { readFileAsText } from '../Utils'
|
||||
import { WebApplicationInterface } from '../../WebApplication/WebApplicationInterface'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
|
||||
type SimplenoteItem = {
|
||||
creationDate: string
|
||||
@@ -14,11 +14,13 @@ type SimplenoteData = {
|
||||
trashedNotes: SimplenoteItem[]
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const isSimplenoteEntry = (entry: any): boolean => entry.id && entry.content && entry.creationDate && entry.lastModified
|
||||
|
||||
export class SimplenoteConverter {
|
||||
constructor(protected application: WebApplicationInterface) {}
|
||||
constructor() {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
static isValidSimplenoteJson(json: any): boolean {
|
||||
return (
|
||||
(json.activeNotes && json.activeNotes.every(isSimplenoteEntry)) ||
|
||||
@@ -53,7 +55,7 @@ export class SimplenoteConverter {
|
||||
created_at_timestamp: createdAtDate.getTime(),
|
||||
updated_at: updatedAtDate,
|
||||
updated_at_timestamp: updatedAtDate.getTime(),
|
||||
uuid: this.application.generateUUID(),
|
||||
uuid: UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.TYPES.Note,
|
||||
content: {
|
||||
title,
|
||||
|
||||
Reference in New Issue
Block a user