-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
54a0af6
commit 4684b95
Showing
21 changed files
with
408 additions
and
40 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
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,49 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { | ||
dynamicRequire, | ||
relativeImport, | ||
} from '@component-controls/core/node-utils'; | ||
import { DocumentData, Document } from '@component-controls/core'; | ||
import { CachedFileResource } from './chached-file'; | ||
|
||
export const getDataFile = (filePath: string): string => { | ||
const parts = filePath.split('.'); | ||
parts.splice( | ||
parts.length - parts.length <= 2 ? 1 : 2, | ||
parts.length <= 2 ? 0 : 1, | ||
'data', | ||
); | ||
const extention = parts[parts.length - 1]; | ||
if (extention.length === 3) { | ||
parts[parts.length - 1] = extention.slice(0, -1); | ||
} | ||
return parts.join('.'); | ||
}; | ||
|
||
export const assignDocumentData = (doc: Document, filePath: string): void => { | ||
const dataFilePath = doc.testData | ||
? path.resolve(path.dirname(filePath), doc.testData) | ||
: getDataFile(filePath); | ||
|
||
if (fs.existsSync(dataFilePath)) { | ||
const cached = new CachedFileResource<DocumentData>( | ||
'data-driven', | ||
`v1`, | ||
filePath, | ||
); | ||
let result: DocumentData | undefined = cached.get(); | ||
if (result) { | ||
doc.data = result; | ||
doc.testData = | ||
doc.testData || relativeImport(path.dirname(filePath), dataFilePath); | ||
return; | ||
} | ||
//load existing data file | ||
const loaded = dynamicRequire(dataFilePath); | ||
result = loaded.default || loaded; | ||
cached.set(result); | ||
doc.data = result; | ||
doc.testData = doc.testData || path.relative(filePath, dataFilePath); | ||
} | ||
}; |
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,43 @@ | ||
import path from 'path'; | ||
import { Document } from '@component-controls/core'; | ||
import { getDataFile, assignDocumentData } from '../src/misc/data-driven'; | ||
|
||
describe('data-driven', () => { | ||
it('getDataFile length 1', () => { | ||
const fileName = getDataFile('file'); | ||
expect(fileName).toBe('file.data'); | ||
}); | ||
it('getDataFile length 2', () => { | ||
const fileName = getDataFile('file.js'); | ||
expect(fileName).toBe('file.data.js'); | ||
}); | ||
|
||
it('getDataFile length > 2', () => { | ||
const fileName = getDataFile('file.test.js'); | ||
expect(fileName).toBe('file.data.js'); | ||
}); | ||
it('load data file', () => { | ||
const doc: Document = { title: 'doc' }; | ||
assignDocumentData( | ||
doc, | ||
path.resolve( | ||
__dirname, | ||
'../../../examples/stories/src/stories/VariantButton/VariantButton.stories.tsx', | ||
), | ||
); | ||
expect(doc.testData).toBe('./VariantButton.data.ts'); | ||
expect(Object.keys(doc.data?.overview).length).toBe(5); | ||
}); | ||
it('load doc testData file', () => { | ||
const doc: Document = { title: 'doc', testData: './VariantButton.data.ts' }; | ||
assignDocumentData( | ||
doc, | ||
path.resolve( | ||
__dirname, | ||
'../../../examples/stories/src/stories/VariantButton/VariantButton.stories.tsx', | ||
), | ||
); | ||
expect(doc.testData).toBe('./VariantButton.data.ts'); | ||
expect(Object.keys(doc.data?.overview).length).toBe(5); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.