nomen-lang 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +45 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nomen-lang",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "The CLI for the Nomen programming language.",
5
5
  "keywords": [],
6
6
  "license": "ISC",
package/src/index.ts CHANGED
@@ -65,15 +65,12 @@ const options = yargs(hideBin(process.argv))
65
65
  type: "string",
66
66
  })
67
67
  .help(true)
68
- .check((argv) => {
69
- if (!argv._.length && !argv.in) {
70
- throw new Error("Missing required argument: in");
71
- }
72
- return true;
73
- })
74
68
  .parseSync();
75
69
 
76
70
  try {
71
+ // An explicit --in wins; otherwise discover what to compile from the
72
+ // working folder — a package.jsonc `entry`, or a lone .nm file.
73
+ options.in = options.in ?? resolve_input();
77
74
  if (!options.in) {
78
75
  process.exit(0);
79
76
  }
@@ -123,6 +120,48 @@ try {
123
120
  console.log("UH", err);
124
121
  }
125
122
 
123
+ function resolve_input(): string | undefined {
124
+ const cwd = process.cwd();
125
+
126
+ // a) A package.jsonc with an `entry` field (the project's main file).
127
+ const config_path = path.join(cwd, "package.jsonc");
128
+ if (fs.existsSync(config_path)) {
129
+ try {
130
+ const raw = fs.readFileSync(config_path, "utf8");
131
+ const json = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
132
+ const parsed = JSON.parse(json);
133
+ if (parsed.entry) {
134
+ return path.resolve(cwd, parsed.entry);
135
+ }
136
+ } catch {
137
+ // ignore malformed package.jsonc and fall through
138
+ }
139
+ }
140
+
141
+ // b) A single .nm file directly in the working folder.
142
+ let nm_files: string[] = [];
143
+ try {
144
+ nm_files = fs.readdirSync(cwd).filter((f) => f.endsWith(".nm"));
145
+ } catch {
146
+ // unreadable working folder
147
+ }
148
+ if (nm_files.length === 1) {
149
+ return path.resolve(cwd, nm_files[0]);
150
+ }
151
+ if (nm_files.length > 1) {
152
+ console.log(
153
+ `Found ${nm_files.length} .nm files in ${cwd}. Specify which to run with --in:\n ` +
154
+ nm_files.join("\n "),
155
+ );
156
+ return undefined;
157
+ }
158
+
159
+ console.log(
160
+ "Nothing to compile. Pass --in <file/folder>, or run inside a folder with a package.jsonc or a .nm file.",
161
+ );
162
+ return undefined;
163
+ }
164
+
126
165
  function resolve_lib(file_path: string): string | undefined {
127
166
  let dir = path.dirname(path.resolve(file_path));
128
167
  for (let i = 0; i < 20; i++) {