Gorniv / vscode-flutter-files

Quickly create files for flutter using a BLoC pattern
https://marketplace.visualstudio.com/items?itemName=gornivv.vscode-flutter-files
MIT License
59 stars 25 forks source link

feat: nested folder and index files #5

Open Gorniv opened 5 years ago

Gorniv commented 5 years ago

add export nested folder

bounty1342 commented 4 years ago

Hi @Gorniv,

I did some modifications to my fork to handle creation of folders. You will find the change below : ressourse-file.ts

import { TemplateType } from './../enums/template-type';
export interface IResourceFile {
  name: Function;
  folder?: Function;
  type: TemplateType;
  condition?: Function;
}

ressource.ts

        {
          name: () => 'view.dart',
          folder: () => '',
          type: TemplateType.ViewTV,
        },

ioutils.ts

const mkdirp = require('mkdirp');
// Create the new folder
export const createSubFolder = async (loc: IPath, config: IConfig, files: IResourceFile[]) => {
  files
    .filter((file) => (file.condition ? file.condition(config, loc.params) : true))
    // tslint:disable-next-line:ter-arrow-parens
    .filter((file) => file.name(config) !== 'index.dart')
    .map(async (file) => {
      try {
        const myPath = { ...loc };
        const fileFolder: string = file.folder(config);
        myPath.dirPath = path.join(myPath.dirPath, fileFolder);
        await mkdirp(myPath.dirPath);
      } catch (ex) {
        await window.showErrorMessage(`Error: ${ex}`);
      }
    });

angular-cli.ts

    // tslint:disable-next-line:ter-arrow-parens
    if (resource.hasOwnProperty('createFolder') && resource.createFolder(config)) {
      await createFolder(loc);
      await createSubFolder(loc, config, resource.files);
    }

    const filesASync: Promise<IFiles>[] = resource.files
      // tslint:disable-next-line:ter-arrow-parens
      .filter((file) => (file.condition ? file.condition(config, loc.params) : true))
      // tslint:disable-next-line:ter-arrow-parens
      .filter((file) => file.name(config) !== 'index.dart')
      .map(async (file) => {
        try {
          const fileName: string = file.name(config);
          const fileFolder: string = file.folder(config);
          const fileFolderAndName: string = path.join(
            fileFolder,
            fileName.startsWith('_') ? `${loc.fileName}${fileName}` : `${loc.fileName}_${fileName}`,
          );

          const newName: string = path.join(loc.dirPath, fileFolderAndName);

          const result: IFiles = {
            name: newName,
            content: await this.fc.getTemplateContent(
              file.type,
              config,
              loc.fileName,
              loc.params,
              loc,
            ),
          };
          return result;
        } catch (ex) {
          console.log(ex);
          await window.showErrorMessage(`Error: ${ex}`);
        }
      });

    const files = await Promise.all(filesASync);
    await createFiles(loc, files);

    const filesIndex: Promise<IFiles>[] = resource.files
      // tslint:disable-next-line:ter-arrow-parens
      .filter((file) => (file.condition ? file.condition(config, loc.params) : true))
      // tslint:disable-next-line:ter-arrow-parens
      .filter((file) => file.name(config) === 'index.dart')
      .map(async (file) => {
        try {
          let contentStr = '';

          resource.files
            // tslint:disable-next-line:ter-arrow-parens
            .filter((dartFile) =>
              dartFile.condition ? dartFile.condition(config, loc.params) : true,
            )
            // tslint:disable-next-line:ter-arrow-parens
            .filter((dartFile) => dartFile.name(config) !== 'index.dart')
            .forEach(function(val) {
              const fileName: string = val.name(config);
              const fileFolder: string = val.folder(config);
              const fileFolderAndName: string = path.join(
                fileFolder,
                fileName.startsWith('_')
                  ? `${loc.fileName}${fileName}`
                  : `${loc.fileName}_${fileName}`,
              );
              const newName: string = path.join(loc.dirPath, fileFolderAndName);
              contentStr += `export '${fileFolderAndName}';\r\n`;
            });

          const fileName: string = file.name(config);

          const result: IFiles = {
            name: path.join(loc.dirPath, loc.fileName + '_' + fileName),
            content: contentStr,
          };
          return result;
        } catch (ex) {
          console.log(ex);
          await window.showErrorMessage(`Error: ${ex}`);
        }
      });
    const indexFiles = await Promise.all(filesIndex);
    await createFiles(loc, indexFiles);
  }

Typescript is not a language I'm familiar with, so they might be better solutions, and why I didn't risk a PR. Hope this help anyway.

PS: You could think of broaden the use of your extension. I use it personally to create the structure of a projet instead of Bloc.