-
Notifications
You must be signed in to change notification settings - Fork 37
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
29 changed files
with
1,228 additions
and
298 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,58 @@ | ||
import { | ||
RevisionContext as RevisionContextTransfrom, | ||
RevisionMeta, | ||
} from '@diplodoc/transform/lib/typings'; | ||
import glob from 'glob'; | ||
import {getMetaFile, makeMetaFile, updateChangedMetaFile, updateMetaFile} from '~/utils/meta'; | ||
|
||
export interface RevisionContext extends RevisionContextTransfrom { | ||
userInputFolder: string; | ||
userOutputFolder: string; | ||
tmpInputFolder: string; | ||
tmpOutputFolder: string; | ||
outputBundlePath: string; | ||
} | ||
|
||
export async function makeRevisionContext( | ||
cached: boolean, | ||
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/**'], | ||
}); | ||
|
||
const meta = normalizeMeta(await getMetaFile(userOutputFolder)); | ||
|
||
await updateMetaFile(cached, userInputFolder, meta.files, files); | ||
|
||
await updateChangedMetaFile(cached, userInputFolder, meta.files); | ||
|
||
return { | ||
userInputFolder, | ||
userOutputFolder, | ||
tmpInputFolder, | ||
tmpOutputFolder, | ||
outputBundlePath, | ||
files, | ||
meta, | ||
}; | ||
} | ||
|
||
function normalizeMeta(meta?: RevisionMeta | undefined | null) { | ||
const metaSafe: RevisionMeta = meta ?? { | ||
files: {}, | ||
}; | ||
metaSafe.files = metaSafe.files ?? {}; | ||
return metaSafe; | ||
} | ||
|
||
export async function setRevisionContext(context: RevisionContext): Promise<void> { | ||
await makeMetaFile(context.userOutputFolder, context.files, context.meta); | ||
} |
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,62 @@ | ||
import {resolve} from 'path'; | ||
import {DependencyContext} from '@diplodoc/transform/lib/typings'; | ||
import {RevisionContext} from './context'; | ||
|
||
export class DependencyContextCli implements DependencyContext { | ||
private context: RevisionContext; | ||
|
||
constructor(context: RevisionContext) { | ||
this.context = context; | ||
} | ||
|
||
getAssetPath(path: string) { | ||
const isFromTmpInputFolder = path.startsWith(resolve(this.context.tmpInputFolder) + '/'); | ||
if (isFromTmpInputFolder) { | ||
const assetPath = path.replace(resolve(this.context.tmpInputFolder) + '/', ''); | ||
return assetPath; | ||
} | ||
|
||
const isFromInputFolder = path.startsWith(resolve(this.context.userInputFolder) + '/'); | ||
if (isFromInputFolder) { | ||
const assetPath = path.replace(resolve(this.context.userInputFolder) + '/', ''); | ||
return assetPath; | ||
} | ||
|
||
return path; | ||
} | ||
|
||
markDep(path: string, dependencyPath: string, type?: string): void { | ||
type = type ?? 'include'; | ||
|
||
const assetPath = this.getAssetPath(path); | ||
const depAssetPath = this.getAssetPath(dependencyPath); | ||
|
||
if (assetPath && depAssetPath && this.context?.meta?.files?.[assetPath]) { | ||
const dependencies = this.context.meta.files[assetPath].dependencies[type] ?? []; | ||
const array = [...dependencies, depAssetPath]; | ||
this.context.meta.files[assetPath].dependencies[type] = [...new Set(array)]; | ||
} | ||
} | ||
|
||
unmarkDep(path: string, dependencyPath: string, type?: string): void { | ||
type = type ?? 'include'; | ||
|
||
const assetPath = this.getAssetPath(path); | ||
const depAssetPath = this.getAssetPath(dependencyPath); | ||
|
||
if (assetPath && depAssetPath && this.context?.meta?.files?.[assetPath]) { | ||
const dependencies = this.context.meta.files[assetPath].dependencies[type] ?? []; | ||
this.context.meta.files[assetPath].dependencies[type] = dependencies.filter( | ||
(file) => file !== depAssetPath, | ||
); | ||
} | ||
} | ||
|
||
resetDeps(path: string): void { | ||
const assetPath = this.getAssetPath(path); | ||
|
||
if (assetPath && this.context?.meta?.files?.[assetPath]) { | ||
this.context.meta.files[assetPath].dependencies = {}; | ||
} | ||
} | ||
} |
Oops, something went wrong.