-
Notifications
You must be signed in to change notification settings - Fork 38
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
19 changed files
with
581 additions
and
287 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {RevisionContext as RevisionContextTransfrom} from '@diplodoc/transform/lib/typings'; | ||
import glob from 'glob'; | ||
|
||
export interface RevisionContext extends RevisionContextTransfrom { | ||
userInputFolder: string; | ||
userOutputFolder: string; | ||
tmpInputFolder: string; | ||
tmpOutputFolder: string; | ||
outputBundlePath: string; | ||
} | ||
|
||
export async function makeRevisionContext( | ||
userInputFolder: string, | ||
userOutputFolder: string, | ||
tmpInputFolder: string, | ||
tmpOutputFolder: string, | ||
outputBundlePath: string, | ||
): Promise<RevisionContext> { | ||
const files = glob.sync('**', { | ||
cwd: userInputFolder, | ||
nodir: true, | ||
follow: true, | ||
ignore: ['node_modules/**', '*/node_modules/**'], | ||
}); | ||
|
||
return { | ||
userInputFolder, | ||
userOutputFolder, | ||
tmpInputFolder, | ||
tmpOutputFolder, | ||
outputBundlePath, | ||
files, | ||
}; | ||
} |
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,107 @@ | ||
import {readFileSync, statSync, writeFileSync} from 'fs'; | ||
import {readFile, stat, writeFile} from 'fs/promises'; | ||
import {resolve} from 'path'; | ||
import {FsContext} from '@diplodoc/transform/lib/typings'; | ||
import {RevisionContext} from './context'; | ||
|
||
export function isFileExists(file: string) { | ||
try { | ||
const stats = statSync(file); | ||
|
||
return stats.isFile(); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
export async function isFileExistsAsync(file: string) { | ||
try { | ||
const stats = await stat(file); | ||
|
||
return stats.isFile(); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
export class FsContextCli implements FsContext { | ||
private context: RevisionContext; | ||
|
||
constructor(context: RevisionContext) { | ||
this.context = context; | ||
} | ||
|
||
getPaths(path: string) { | ||
const arr = [path]; | ||
|
||
const isFromTmpInputFolder = path.startsWith(resolve(this.context.tmpInputFolder) + '/'); | ||
if (isFromTmpInputFolder) { | ||
const assetPath = path.replace(resolve(this.context.tmpInputFolder) + '/', ''); | ||
const originPath = resolve(this.context.userInputFolder, assetPath); | ||
|
||
arr.unshift(originPath); | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
exist(path: string): boolean { | ||
const paths = this.getPaths(path); | ||
|
||
for (const path of paths) { | ||
if (isFileExists(path)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
read(path: string): string { | ||
const paths = this.getPaths(path); | ||
|
||
for (const path of paths) { | ||
if (isFileExists(path)) { | ||
return readFileSync(path, 'utf8'); | ||
} | ||
} | ||
|
||
throw Error(`File has not been found at: ${path}`); | ||
} | ||
|
||
write(path: string, content: string): void { | ||
writeFileSync(path, content, { | ||
encoding: 'utf8', | ||
}); | ||
} | ||
|
||
async existAsync(path: string): Promise<boolean> { | ||
const paths = this.getPaths(path); | ||
|
||
for (const path of paths) { | ||
if (await isFileExistsAsync(path)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
async readAsync(path: string): Promise<string> { | ||
const paths = this.getPaths(path); | ||
|
||
for (const path of paths) { | ||
if (await isFileExistsAsync(path)) { | ||
return await readFile(path, 'utf8'); | ||
} | ||
} | ||
|
||
throw Error(`File has not been found at: ${path}`); | ||
} | ||
|
||
async writeAsync(path: string, content: string): Promise<void> { | ||
await writeFile(path, content, { | ||
encoding: 'utf8', | ||
}); | ||
} | ||
} |
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.