Skip to content

Commit

Permalink
Merge branch 'release/1.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
btassone committed Oct 11, 2018
2 parents c80008d + b218cb8 commit 4e18149
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ siteMetaFileName: string - The file name that holds all the meta information abo
siteMetaPath: string - The location the meta file should be generated.
[Default]: .
importPath: string - If siteMetaPath is not the root sometimes the import path needs to change
[Default]: ''
ignoreFiles: string[] - List of file names (case-sensitive) without the ts extension.
[Default]: []
Expand Down
52 changes: 51 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const encoding = 'utf8';

/**
* Webpack plugin that generates a container object and associates the class name with its reference for easy access
*
* @version 1.0.6
*/
class TypescriptClassMetaInfoGeneratorPlugin {
/**
Expand All @@ -22,6 +24,8 @@ class TypescriptClassMetaInfoGeneratorPlugin {
* [Default]: site-meta.ts
* siteMetaPath: string - The location the meta file should be generated.
* [Default]: .
* importPath: string - If siteMetaPath is not the root sometimes the import path needs to change
* [Default]: ''
* ignoreFiles: string[] - List of file names (case-sensitive) without the ts extension.
* [Default]: []
* ignoreFolders: string[] - List of folder names (case-sensitive) to ignore.
Expand All @@ -32,6 +36,7 @@ class TypescriptClassMetaInfoGeneratorPlugin {
this.siteName = (options.siteName) ? options.siteName : 'Site';
this.siteMetaFileName = (options.siteMetaFileName) ? options.siteMetaFileName : 'site-meta.ts';
this.siteMetaPath = (options.siteMetaPath) ? options.siteMetaPath : '.';
this.importPath = (options.importPath) ? options.importPath : '';
this.ignoreFiles = (options.ignoreFiles) ? options.ignoreFiles : [];
this.ignoreFolders = (options.ignoreFolders) ? options.ignoreFolders: [];
}
Expand Down Expand Up @@ -84,14 +89,45 @@ class TypescriptClassMetaInfoGeneratorPlugin {
this.output.container = `\nexport let ${this.siteName}: any = {};\n`;

this.files.forEach(file => {
this.output.imports.push(`import { ${file.name} } from "${file.path}";\n`);
this.output.imports.push(`import { ${file.name} } from "${this.createImportPath(file.path)}";\n`);
this.output.associations.push(`\n${this.siteName}.${file.name} = ${file.name};`);
});

fs.writeFileSync(this.siteMetaFullPath, this.output.combined(), encoding);
});
}

/**
* Gets the absolute path to file and removes the srcFolder path part to leave just the relative path to the file.
* Once obtained it appends it to the specified importPath
*
* @param {string} filePath
*
* @returns {string}
*/
createImportPath(filePath) {
let importPath = filePath;

if(this.importPath !== "") {
let relativePath = this.arrayDif(filePath.split(path.sep), this.srcFolder.split(path.sep)).join(path.sep);
importPath = `${this.importPath}${path.sep}${relativePath}`;
}

return importPath;
}

/**
* Finds the differences from the first array in the second
*
* @param {array} a1
* @param {array} a2
*
* @returns {array}
*/
arrayDif(a1, a2) {
return a1.filter(item => a2.indexOf(item) < 0);
}

/**
* Returns the siteMetaPath joined with the siteMetaFileName
*
Expand Down Expand Up @@ -143,6 +179,20 @@ class TypescriptClassMetaInfoGeneratorPlugin {
this._siteMetaPath = value;
}

/**
* @returns {string}
*/
get importPath() {
return this._importPath;
}

/**
* @param {string} value
*/
set importPath(value) {
this._importPath = value;
}

/**
* @returns {string}
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-class-meta-generator",
"version": "1.0.5",
"version": "1.0.6",
"description": "Webpack plugin that generates a container object and associates the class name with its reference for easy access",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4e18149

Please sign in to comment.