Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nested folder and index files #5

Open
Gorniv opened this issue Apr 1, 2019 · 2 comments
Open

feat: nested folder and index files #5

Gorniv opened this issue Apr 1, 2019 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@Gorniv
Copy link
Owner

Gorniv commented Apr 1, 2019

add export nested folder

@Gorniv Gorniv self-assigned this Apr 1, 2019
@Gorniv Gorniv added the enhancement New feature or request label May 7, 2019
@bounty1342
Copy link
Contributor

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.

@fadizant
Copy link

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.

Hi bounty1342,

Can I have an access to this fork, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants