Skip to content

Commit

Permalink
refactor(app-shell): Migrate protocols to "protocols" directory (#13753)
Browse files Browse the repository at this point in the history
* refactor(app-shell): perform protocol migration if pre-parity v7.0 protocols folder exists

Migrates protocols to the protocols directory if protocols_v7.0-supported directory exists.
  • Loading branch information
mjhuff authored Oct 11, 2023
1 parent c81dec0 commit afffc45
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 268 deletions.
8 changes: 3 additions & 5 deletions app-shell/src/protocol-storage/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ import { analyzeProtocolSource } from '../protocol-analysis'
* │ ├─ analysis/
* │ │ ├─ 1646303906.json
*/
// TODO(jh, 2023-09-11): remove OLD_PROTOCOLS_DIRECTORY_PATH after
// OT-2 parity work is completed and move all protocols back to "protocols" directory.
export const OLD_PROTOCOLS_DIRECTORY_PATH = path.join(
export const PRE_V7_PARITY_DIRECTORY_PATH = path.join(
app.getPath('userData'),
'protocols'
'protocols_v7.0-supported'
)
export const PROTOCOLS_DIRECTORY_NAME = 'protocols_v7.0-supported'
export const PROTOCOLS_DIRECTORY_NAME = 'protocols'
export const PROTOCOLS_DIRECTORY_PATH = path.join(
app.getPath('userData'),
PROTOCOLS_DIRECTORY_NAME
Expand Down
88 changes: 45 additions & 43 deletions app-shell/src/protocol-storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,35 @@ export const getProtocolSrcFilePaths = (
})
}

// TODO(jh, 2023-09-11): remove migrateProtocolsToNewDirectory after
// OT-2 parity work is completed.
const migrateProtocols = migrateProtocolsToNewDirectory()
function migrateProtocolsToNewDirectory(): () => Promise<void> {
// Revert a v7.0.0 pre-parity stop-gap solution.
const migrateProtocolsFromTempDirectory = preParityMigrateProtocolsFrom(
FileSystem.PRE_V7_PARITY_DIRECTORY_PATH,
FileSystem.PROTOCOLS_DIRECTORY_PATH
)
export function preParityMigrateProtocolsFrom(
src: string,
dest: string
): () => Promise<void> {
let hasCheckedForMigration = false

return function (): Promise<void> {
return new Promise((resolve, reject) => {
if (hasCheckedForMigration) resolve()
hasCheckedForMigration = true
console.log(
`Performing protocol migration to ${FileSystem.PROTOCOLS_DIRECTORY_NAME}...`
)
copyProtocols(
FileSystem.OLD_PROTOCOLS_DIRECTORY_PATH,
FileSystem.PROTOCOLS_DIRECTORY_PATH
)
.then(() => {
console.log('Protocol migration complete.')
resolve()

fse
.stat(src)
.then(doesSrcExist => {
if (!doesSrcExist.isDirectory()) resolve()

console.log(

Check warning on line 66 in app-shell/src/protocol-storage/index.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/protocol-storage/index.ts#L66

Added line #L66 was not covered by tests
`Performing protocol migration to ${FileSystem.PROTOCOLS_DIRECTORY_NAME}...`
)

return migrateProtocols(src, dest).then(() => {
console.log('Protocol migration complete.')
resolve()

Check warning on line 72 in app-shell/src/protocol-storage/index.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/protocol-storage/index.ts#L70-L72

Added lines #L70 - L72 were not covered by tests
})
})
.catch(e => {
console.log(
Expand All @@ -71,27 +81,27 @@ function migrateProtocolsToNewDirectory(): () => Promise<void> {
})
}

function copyProtocols(src: string, dest: string): Promise<void> {
function migrateProtocols(src: string, dest: string): Promise<void> {
return fse
.stat(src)
.then(doesSrcExist => {
if (!doesSrcExist.isDirectory()) return Promise.resolve()

return fse.readdir(src).then(items => {
const protocols = items.map(item => {
const srcItem = path.join(src, item)
const destItem = path.join(dest, item)

return fse.copy(srcItem, destItem, {
overwrite: false,
})
.readdir(src)
.then(items => {
const protocols = items.map(item => {
const srcItem = path.join(src, item)
const destItem = path.join(dest, item)

Check warning on line 90 in app-shell/src/protocol-storage/index.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/protocol-storage/index.ts#L88-L90

Added lines #L88 - L90 were not covered by tests

return fse.copy(srcItem, destItem, {

Check warning on line 92 in app-shell/src/protocol-storage/index.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/protocol-storage/index.ts#L92

Added line #L92 was not covered by tests
overwrite: false,
})
return Promise.all(protocols).then(() => Promise.resolve())
})
// Delete the tmp directory.
return Promise.all(protocols).then(() =>
fse.rm(src, {

Check warning on line 98 in app-shell/src/protocol-storage/index.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/protocol-storage/index.ts#L97-L98

Added lines #L97 - L98 were not covered by tests
recursive: true,
force: true,
})
)
})
.catch(e => {
return Promise.reject(e)
})
.catch(e => Promise.reject(e))

Check warning on line 104 in app-shell/src/protocol-storage/index.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/protocol-storage/index.ts#L104

Added line #L104 was not covered by tests
}
}

Expand All @@ -100,7 +110,7 @@ export const fetchProtocols = (
source: ListSource
): Promise<void> => {
return ensureDir(FileSystem.PROTOCOLS_DIRECTORY_PATH)
.then(() => migrateProtocols())
.then(() => migrateProtocolsFromTempDirectory())
.then(() =>
FileSystem.readDirectoriesWithinDirectory(
FileSystem.PROTOCOLS_DIRECTORY_PATH
Expand Down Expand Up @@ -201,22 +211,14 @@ export function registerProtocolStorage(dispatch: Dispatch): Dispatch {
})
break
}
// TODO(jh, 2023-09-15): remove the secondary removeProtocolByKey() after
// OT-2 parity work is completed.

case ProtocolStorageActions.REMOVE_PROTOCOL: {
FileSystem.removeProtocolByKey(
action.payload.protocolKey,
FileSystem.PROTOCOLS_DIRECTORY_PATH
).then(() =>
fetchProtocols(dispatch, ProtocolStorageActions.PROTOCOL_ADDITION)

Check warning on line 220 in app-shell/src/protocol-storage/index.ts

View check run for this annotation

Codecov / codecov/patch

app-shell/src/protocol-storage/index.ts#L220

Added line #L220 was not covered by tests
)
.then(() =>
fetchProtocols(dispatch, ProtocolStorageActions.PROTOCOL_ADDITION)
)
.then(() =>
FileSystem.removeProtocolByKey(
action.payload.protocolKey,
FileSystem.OLD_PROTOCOLS_DIRECTORY_PATH
)
)
break
}

Expand Down
Loading

0 comments on commit afffc45

Please sign in to comment.