2
0

get-status.ts 844 B

1234567891011121314151617181920212223242526272829
  1. import fs, { readFileSync } from 'fs';
  2. import path from 'path';
  3. import { promisify } from 'util';
  4. const statAsync = promisify(fs.stat);
  5. type TemplateDirStatus = {
  6. isTemplateExists: boolean,
  7. meta?: { [key: string]: string },
  8. }
  9. export async function getStatus(tplDir: string): Promise<TemplateDirStatus> {
  10. const markdownPath = path.resolve(tplDir, 'template.md');
  11. const statForMarkdown = await statAsync(markdownPath);
  12. const isTemplateExists = statForMarkdown.isFile();
  13. const metaDataPath = path.resolve(tplDir, 'meta.json');
  14. const statForMetaDataFile = await statAsync(metaDataPath);
  15. const isMetaDataFileExists = statForMetaDataFile.isFile();
  16. const result: TemplateDirStatus = {
  17. isTemplateExists,
  18. meta: isMetaDataFileExists ? JSON.parse(readFileSync(metaDataPath, 'utf-8')) : undefined,
  19. };
  20. return result;
  21. }