Skip to content

Add node module resolution by default and use --path for custom package locations #594

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

Merged
merged 55 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
b8e8ee1
Added all dependencies to library files
willemneal Apr 16, 2019
c161742
Works but requires reading in all files
willemneal May 17, 2019
ff91130
Working
willemneal May 17, 2019
a380ed4
freeze
willemneal May 17, 2019
277853e
Working but need to add test
willemneal May 17, 2019
1dae71a
Merge remote-tracking branch 'upstream/master' into module_resolution
willemneal Jun 6, 2019
ef9ebdf
Merge remote-tracking branch 'upstream/master' into module_resolution
willemneal Jun 13, 2019
0c42af4
Updated to lazily find packages
willemneal Jun 13, 2019
af33db0
Update dist files
willemneal Jun 13, 2019
cd875dc
Added tests
willemneal Jun 14, 2019
6ffb5ff
Fixed output for resolution trace and file list
willemneal Jun 14, 2019
5869b57
Reverted dist files
willemneal Jun 14, 2019
b9a29b7
Actually revert dist files and add PR to tests
willemneal Jun 14, 2019
0e3f1a8
I really mean it this time
willemneal Jun 14, 2019
671d49c
For some reason it says it doesn't exist on master...
willemneal Jun 14, 2019
7d4e478
Copied from master
willemneal Jun 14, 2019
deb8942
forgot a space
willemneal Jun 14, 2019
92e03e5
change but it says file contents unchanged
willemneal Jun 14, 2019
29ac93e
Changed output to stderr and removed template literals
willemneal Jun 14, 2019
9b8a1d3
Removed all dependencies
willemneal Jun 14, 2019
79c08f2
Update .gitignore
willemneal Jun 15, 2019
84d2429
Update .gitignore
willemneal Jun 15, 2019
e5beacf
Format comment
willemneal Jun 15, 2019
117ce01
Fix PR issues
willemneal Jun 19, 2019
c7f6ad0
wrapped JSON.parse in a try/catch
Jun 19, 2019
1c86edc
nit fixes
willemneal Jun 20, 2019
edcf1a0
Search node_modules by default
willemneal Jun 21, 2019
78907a1
Merge remote-tracking branch 'upstream/master' into module_resolution
willemneal Jun 28, 2019
50c2342
Add dependee to backlog
willemneal Jun 28, 2019
1b0c4d7
imports start search from dependee
willemneal Jul 1, 2019
aad9ed2
switch to destructuring instead of flat()
willemneal Jul 1, 2019
9f42e76
Changed back to C-like API
willemneal Jul 1, 2019
df4f0df
Removed use of unix test command
willemneal Jul 4, 2019
aa92094
Change script field and change to regex.test
willemneal Jul 4, 2019
f36d31d
Travis test
willemneal Jul 4, 2019
915761c
simplified regex and added flag
willemneal Jul 4, 2019
fad255a
Print out more info
willemneal Jul 4, 2019
8db83c6
Try again
willemneal Jul 4, 2019
4dba3d8
Should work now
willemneal Jul 4, 2019
1795bca
Made hard copies of dependencies for better windows support
willemneal Jul 5, 2019
204791e
formatting [skip ci]
willemneal Jul 5, 2019
9aa137b
Removed using npm all together
willemneal Jul 8, 2019
55b7f05
Removed all package.json files and scripts
willemneal Jul 12, 2019
02d847a
formatting
willemneal Jul 12, 2019
c7496c2
removed sysPaths from parser and moved to map in asc
Jul 12, 2019
87b6b5f
Changed to map paths to sources
Jul 12, 2019
bffa751
formatting and replaced regex replace
Jul 12, 2019
78a84b3
Fix sep constant and g's test
Jul 12, 2019
afe3276
Removed default baseDir in nodeFileRead
Jul 12, 2019
040fe22
Last nit fixes
Jul 15, 2019
e4ca804
Formatting
willemneal Jul 16, 2019
1e29ad2
[skip ci]
willemneal Jul 16, 2019
4a53df2
Improved trace information
willemneal Jul 16, 2019
ed7eb64
fix split of internal path to `/`
willemneal Jul 16, 2019
2c94848
nit [skip ci]
willemneal Jul 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules/
out/
raw/
.history

tests/packages/lerna-debug\.log
71 changes: 69 additions & 2 deletions cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ exports.main = function main(argv, options, callback) {

// Begin parsing
var parser = null;

//maps package names to parent directory
let packages = new Map();

// Include library files
Object.keys(exports.libraryFiles).forEach(libPath => {
Expand Down Expand Up @@ -261,6 +264,7 @@ exports.main = function main(argv, options, callback) {
let libText = readFile(libPath, libDir);
if (libText === null) return callback(Error("Library file '" + libPath + "' not found."));
stats.parseCount++;
exports.libraryFiles[libPath.replace(/\.ts$/, "")] = libText
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(
libText,
Expand Down Expand Up @@ -342,7 +346,62 @@ exports.main = function main(argv, options, callback) {
}
}
}
if (sourceText == null) {
/*
In this case the library wasn't found so we check paths
*/
if (sourceText == null && args.path) {
writeStdout(`Looking for ${sourcePath}\n`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should go to stderr - stdout might be module output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay will change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that should also be the case for traceResolution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I think that the debugging might help now to implement the feature, but I'm skeptical that it deserves actual compiler options that nobody will ever use, unless debugging the feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well typescript has the option and I did need a way to debug it. I'm not attached though. tsc also has --all which is where you'll find these types of commands not just --help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, hiding these behind an -all flag sounds good, yeah.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add that now or do a future PR?

for (let _path of args.path) {
let _package = sourcePath.replace(/\~lib\/([^\/]*).*/, `$1`);
if (args.traceResolution) {
writeStdout(`in ${_path}`);
}
let ascMain = (() => {
if (packages.has(_package)){
return packages.get(_package);
}
let p = path.join(_path, _package, "package.json");
let res = readFile(p, baseDir);
if (res){
let mainFile = JSON.parse(res).ascMain
if (mainFile){
let newPackage = mainFile.replace(/(.*)\/index\.ts/, '$1');
packages.set(_package, newPackage);
return newPackage;
}
}
return "assembly";
})()
let realPath = (_p) => {
return _p.replace(/\~lib\/([^/]*)\/(.*)/, `${_path}/$1/${ascMain}/$2`);
}
const plainName = sourcePath;
const indexName = sourcePath + "/index";
sourceText = readFile(realPath(plainName) + ".ts", baseDir);
if (sourceText !== null) {
sourcePath = plainName + ".ts";
} else {
sourceText = readFile(realPath(indexName) + ".ts", baseDir);
if (sourceText !== null) {
sourcePath = indexName + ".ts";
}
}
if (sourceText !== null) {
if (args.traceResolution) {
writeStdout(` -> Found at ${realPath(sourcePath)}\n`);
}
let newPath = path.join(_path, _package, "node_modules");
if (!args.path.includes(newPath)){
args.path.push(newPath);
}
break;
}
if (args.traceResolution) {
writeStdout('\n');
}
}
}
if (sourceText == null){
return callback(Error("Import file '" + sourcePath + ".ts' not found."));
}
stats.parseCount++;
Expand Down Expand Up @@ -418,6 +477,12 @@ exports.main = function main(argv, options, callback) {
// Finish parsing
const program = assemblyscript.finishParsing(parser);

// Print files and exit if listFiles
if (args.listFiles) {
writeStdout(program.sources.map(s => s.normalizedPath).join("\n") + "\n");
callback(null);
}

// Set up optimization levels
var optimizeLevel = 0;
var shrinkLevel = 0;
Expand Down Expand Up @@ -718,11 +783,13 @@ exports.main = function main(argv, options, callback) {
return callback(null);

function readFileNode(filename, baseDir) {
let dir = baseDir || "/"
let name = path.resolve(path.join(dir, filename));
try {
let text;
stats.readCount++;
stats.readTime += measure(() => {
text = fs.readFileSync(path.join(baseDir, filename), { encoding: "utf8" });
text = fs.readFileSync(name, { encoding: "utf8" });
});
return text;
} catch (e) {
Expand Down
19 changes: 18 additions & 1 deletion cli/asc.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,22 @@
"-O0z": { "value": { "optimizeLevel": 0, "shrinkLevel": 2 } },
"-O1z": { "value": { "optimizeLevel": 1, "shrinkLevel": 2 } },
"-O2z": { "value": { "optimizeLevel": 2, "shrinkLevel": 2 } },
"-O3z": { "value": { "optimizeLevel": 3, "shrinkLevel": 2 } }
"-O3z": { "value": { "optimizeLevel": 3, "shrinkLevel": 2 } },
"path": {
"description": [
"comma separated paths to look for dependencies.",
"Looks for folders with package.json that includes a 'ascMain' field",
"or defaults to having an '/assembly' folder"],
"type": "S"
},
"traceResolution": {
"description": "Enable tracing of package resolution",
"type": "b",
"default": false
},
"listFiles": {
"description": "List files to be compiled and exit",
"type": "b",
"default": false
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
"check": "npm run check:config && npm run check:compiler",
"check:config": "tsc --noEmit -p src --diagnostics --listFiles",
"check:compiler": "tslint -c tslint.json --project src --formatters-dir lib/lint/formatters --format as",
"test": "npm run test:parser && npm run test:compiler",
"test": "npm run test:parser && npm run test:compiler && npm run test:packages",
"test:parser": "node tests/parser",
"test:compiler": "node tests/compiler",
"test:packages": "node tests/packages",
"make": "npm run clean && npm test && npm run build && npm test",
"all": "npm run check && npm run make",
"docs": "typedoc --tsconfig tsconfig-docs.json --mode modules --name \"AssemblyScript Compiler API\" --out ./docs/api --ignoreCompilerErrors --excludeNotExported --excludePrivate --excludeExternals --exclude **/std/** --includeDeclarations --readme src/README.md",
Expand Down
20 changes: 20 additions & 0 deletions tests/packages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var cp = require('child_process')

// Determine OS and set command accordingly
const cmd = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';

cp.spawn(cmd, ['i'], { env: process.env, cwd: __dirname, stdio: 'inherit' })
.on("error", () => exit(1))
.on("close", (code, signal) => {
if (code != 0 ){
exit(1);
}
cp.spawn(cmd, ['run', 'test'], { env: process.env, cwd: __dirname, stdio: 'inherit' })
.on("error", () => exit(1))
.on("close", (code, signal) => {
if (code != 0 ){
exit(1);
}
});
});

6 changes: 6 additions & 0 deletions tests/packages/lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"packages": [
"packages/*"
],
"version": "0.0.0"
}
Loading