Skip to content

Commit

Permalink
support absolute paths for dist
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Jan 10, 2025
1 parent a03dc11 commit 9668ee6
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Programming platform for Quantitative Systems Pharmacology modeling in NodeJS",
"main": "src/index.js",
"scripts": {
"test:dev": "mocha test/time-switcher/bind-error --config=./test/.mocharc.json",
"test:dev": "mocha test/filepaths --config=./test/.mocharc.json",
"test": "mocha test --config=./test/.mocharc.json",
"jsdoc": "jsdoc -r -c .jsdoc.json --readme api-references.md -d docs/dev src",
"test:cov": "nyc --reporter=lcov npm run test",
Expand Down
18 changes: 13 additions & 5 deletions src/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ class Builder {
Object.assign(this, declaration);

// all relative or absolute depending on coreDirname
this._coreDirname = coreDirname; // already normalized
this._distDirname = path.join(this._coreDirname, declaration.options.distDir); // TODO: HOW TO MAKE relative from absolute
this._metaDirname = path.join(this._coreDirname, declaration.options.metaDir);
this._logPath = path.join(this._coreDirname, declaration.options.logPath);
this._coreDirname = coreDirname; // relative to the shell or absolute
this._distDirname = path.isAbsolute(declaration.options.distDir)
? declaration.options.distDir // absolute path
: path.join(this._coreDirname, declaration.options.distDir); // relative to the shell
this._metaDirname = path.isAbsolute(declaration.options.metaDir)
? declaration.options.metaDir // absolute path
: path.join(this._coreDirname, declaration.options.metaDir); // relative to the shell
this._logPath = path.isAbsolute(declaration.options.logPath)
? declaration.options.logPath // absolute path
: path.join(this._coreDirname, declaration.options.logPath); // relative to the shell

logger.info(`Builder initialized for the platform "${this.id}"`);

Expand Down Expand Up @@ -136,7 +142,9 @@ class Builder {
run() {
// 1. Parsing
let ms = new ModuleSystem(this.logger, this.fileReadHandler);
let absFilename = path.join(this._coreDirname, this.importModule.source);
let absFilename = path.isAbsolute(this.importModule.source)
? this.importModule.source // absolute path
: path.join(this._coreDirname, this.importModule.source); // relative to the shell
ms.addModuleDeep(absFilename, this.importModule.type, this.importModule);

// 2. Modules integration
Expand Down
148 changes: 148 additions & 0 deletions test/filepaths/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* global describe, it */

// check calculated paths

const path = require('path');
const { expect } = require('chai');
const { Builder } = require('../../src/builder');

describe('Check Builder paths with defailt options', () => {
it('Empty coreDirName', () => {
const b = new Builder();

expect(b._coreDirname).to.be.equal('.');
expect(b._distDirname).to.be.equal('dist');
expect(b._metaDirname).to.be.equal('meta');
expect(b._logPath).to.be.equal('build.log');
});

it('Relative coreDirName linux', () => {
const b = new Builder({}, 'test/dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build.log'));
});

it('Relative coreDirName windows', () => {
const b = new Builder({}, 'test\\dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build.log'));
});

it('Absolute coreDirName linux', () => {
const b = new Builder({}, '/test/dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('/test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/test/dir/dist'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/test/dir/meta'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/test/dir/build.log'));
});

it('Absolute coreDirName windows', () => {
const b = new Builder({}, 'C:\\test\\dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('C:/test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('C:/test/dir/dist'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('C:/test/dir/meta'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('C:/test/dir/build.log'));
});
});

describe('Check Builder paths with relative dist dir', () => {
it('Empty coreDirName', () => {
const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}});

expect(b._coreDirname).to.be.equal('.');
expect(b._distDirname).to.be.equal('dist2');
expect(b._metaDirname).to.be.equal('meta2');
expect(b._logPath).to.be.equal('build2.log');
});

it('Relative coreDirName linux', () => {
const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, 'test/dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build2.log'));
});

it('Relative coreDirName windows', () => {
const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, 'test\\dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build2.log'));
});

it('Absolute coreDirName linux', () => {
const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, '/test/dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('/test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/test/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/test/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/test/dir/build2.log'));
});

it('Absolute coreDirName windows', () => {
const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, 'C:\\test\\dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('C:/test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('C:/test/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('C:/test/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('C:/test/dir/build2.log'));
});
});

describe('Check Builder paths with absolute dist dir', () => {
it('Empty coreDirName', () => {
const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}});

expect(b._coreDirname).to.be.equal('.');
expect(b._distDirname).to.be.equal('/some/dir/dist2');
expect(b._metaDirname).to.be.equal('/some/dir/meta2');
expect(b._logPath).to.be.equal('/some/dir/build2.log');
});

it('Relative coreDirName linux', () => {
const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, 'test/dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log'));
});

it('Relative coreDirName windows', () => {
const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, 'test\\dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log'));
});

it('Absolute coreDirName linux', () => {
const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, '/test/dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('/test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log'));
});

it('Absolute coreDirName windows', () => {
const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, 'C:\\test\\dir');

expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('C:/test/dir'));
expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2'));
expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2'));
expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log'));
});
});

0 comments on commit 9668ee6

Please sign in to comment.