Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
サブディレクトリに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
ienaga committed May 19, 2023
1 parent 694621a commit d83d807
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 58 deletions.
175 changes: 118 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,66 @@ module.exports = class Next2DWebpackAutoLoaderPlugin
}
}

/**
* @param {string} path
* @return {string}
*/
_$getFileType (path)
{
try {

const stat = fs.statSync(path);

switch (true) {
case stat.isFile():
return "file";

case stat.isDirectory():
return "directory";

default:
return "unknown";
}

} catch (e) {

return "unknown";

}
}

/**
* @param {string} dir_path
* @return {array}
* @private
*/
_$listFiles (dir_path)
{
const files = [];
const paths = fs.readdirSync(dir_path);

for (let idx = 0; idx < paths.length; ++idx) {

const path = `${dir_path}/${paths[idx]}`;
switch (this._$getFileType(path)) {

case "file":
files.push(path);
break;

case "directory":
files.push(...this._$listFiles(path));
break;

default:
break;

}
}

return files;
};

Check warning on line 176 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Unnecessary semicolon

Check warning on line 176 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Unnecessary semicolon

Check warning on line 176 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Unnecessary semicolon

Check warning on line 176 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Unnecessary semicolon

/**
*
* @param {string} dir
Expand Down Expand Up @@ -175,71 +235,72 @@ module.exports = class Next2DWebpackAutoLoaderPlugin
);
}

glob(`${dir}/src/**/*.js`, (err, files) =>
{
console.log("cwd: ", dir);
if (err) {
throw err;
const files = this._$listFiles(`${dir}/src`);
let imports = "";
let packages = `[${os.EOL}`;
for (let idx = 0; idx < files.length; ++idx) {

const file = files[idx];
if (file.indexOf(".js") === -1) {
continue;
}

let imports = "";
let packages = `[${os.EOL}`;
console.log("Files: ", files);
files.forEach((file) =>
{
const js = fs.readFileSync(file, { "encoding": "utf-8" });
const lines = js.split("\n");
const js = fs.readFileSync(file, { "encoding": "utf-8" });
const lines = js.split("\n");

const path = file.replace(`${dir}/`, "");
lines.forEach((line) =>
{
if (line.startsWith("export class ")) {

const name = line.split(" ")[2];
switch (true) {

case path.indexOf("src/view/") > -1:
imports += `import { ${name} } from "/src/${path.split("src/")[1].split(".js")[0]}";${os.EOL}`;
packages += `["${name}", ${name}],${os.EOL}`;
break;

case path.indexOf("src/model/") > -1:
{
const key = file
.split("src/model/")[1]
.split("/")
.join(".")
.slice(0, -3);

const asName = file
.split("src/model/")[1]
.split("/")
.join("_")
.slice(0, -3);

imports += `import { ${name} as ${asName} } from "/src/${path.split("src/")[1].split(".js")[0]}";${os.EOL}`;
packages += `["${key}", ${asName}],${os.EOL}`;
}
break;
const path = file.replace(`${dir}/`, "");
for (let idx = 0; idx < lines.length; ++idx) {

default:
break;
const line = lines[idx];
if (line.indexOf("export class ") === -1) {
continue;
}

}
const name = line.split(" ")[2];
switch (true) {

case path.indexOf("src/view/") > -1:
imports += `import { ${name} } from "/src/${path.split("src/")[1].split(".js")[0]}";${os.EOL}`;
packages += `["${name}", ${name}],${os.EOL}`;
break;

case path.indexOf("src/model/") > -1:
{

Check failure on line 268 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 24 spaces but found 20

Check failure on line 268 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 24 spaces but found 20

Check failure on line 268 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 24 spaces but found 20

Check failure on line 268 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 24 spaces but found 20
const key = file

Check failure on line 269 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 28 spaces but found 24

Check failure on line 269 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 28 spaces but found 24

Check failure on line 269 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 28 spaces but found 24

Check failure on line 269 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 28 spaces but found 24
.split("src/model/")[1]

Check failure on line 270 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 270 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 270 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 270 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28
.split("/")

Check failure on line 271 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 271 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 271 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 271 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28
.join(".")

Check failure on line 272 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 272 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 272 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 272 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28
.slice(0, -3);

Check failure on line 273 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 273 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 273 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 273 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

const asName = file

Check failure on line 275 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 28 spaces but found 24

Check failure on line 275 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 28 spaces but found 24

Check failure on line 275 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 28 spaces but found 24

Check failure on line 275 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 28 spaces but found 24
.split("src/model/")[1]

Check failure on line 276 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 276 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 276 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 276 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28
.split("/")

Check failure on line 277 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 277 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 277 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 277 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28
.join("_")

Check failure on line 278 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 278 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 278 in index.js

View workflow job for this annotation

GitHub Actions / windows-browser-test

Expected indentation of 32 spaces but found 28

Check failure on line 278 in index.js

View workflow job for this annotation

GitHub Actions / macos-browser-test

Expected indentation of 32 spaces but found 28
.slice(0, -3);

imports += `import { ${name} as ${asName} } from "/src/${path.split("src/")[1].split(".js")[0]}";${os.EOL}`;
packages += `["${key}", ${asName}],${os.EOL}`;
}
});
});
break;

packages = packages.slice(0, -2);
packages += `${os.EOL}]`;
default:
break;

}

break;

const value = `${imports}const packages=${packages};${os.EOL}export { packages };`;
console.log("Export: ", this._$cachePackages, value);
if (this._$cachePackages !== value) {
// cache
this._$cachePackages = value;
fs.writeFileSync(`${dir}/src/Packages.js`, value);
}
});
}

packages = packages.slice(0, -2);
packages += `${os.EOL}]`;

const value = `${imports}const packages=${packages};${os.EOL}export { packages };`;
if (this._$cachePackages !== value) {
// cache
this._$cachePackages = value;
fs.writeFileSync(`${dir}/src/Packages.js`, value);
}
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@next2d/webpack-auto-loader-plugin",
"description": "Next2D Framework webpack Auto Loader plugin.",
"version": "1.6.2",
"version": "1.6.3",
"author": "Toshiyuki Ienaga <ienaga@tvon.jp>",
"license": "MIT",
"main": "index.js",
Expand Down

0 comments on commit d83d807

Please sign in to comment.