@axintai/compiler 0.3.1 → 0.3.2

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.
package/dist/cli/index.js CHANGED
@@ -1163,6 +1163,142 @@ var init_compiler = __esm({
1163
1163
  }
1164
1164
  });
1165
1165
 
1166
+ // src/core/format.ts
1167
+ var format_exports = {};
1168
+ __export(format_exports, {
1169
+ SWIFT_FORMAT_CONFIG: () => SWIFT_FORMAT_CONFIG,
1170
+ formatSwift: () => formatSwift
1171
+ });
1172
+ import { spawn } from "child_process";
1173
+ import { writeFile, unlink } from "fs/promises";
1174
+ import { join } from "path";
1175
+ import { tmpdir } from "os";
1176
+ async function formatSwift(source, options = {}) {
1177
+ const available = await hasSwiftFormat();
1178
+ if (!available) {
1179
+ if (options.strict) {
1180
+ throw new Error(
1181
+ "swift-format not found on $PATH. Install Xcode + Command Line Tools, or drop --format."
1182
+ );
1183
+ }
1184
+ return {
1185
+ formatted: source,
1186
+ ran: false,
1187
+ reason: "swift-format not found on $PATH"
1188
+ };
1189
+ }
1190
+ const configPath = join(
1191
+ tmpdir(),
1192
+ `axint-swift-format-${Date.now()}-${Math.random().toString(36).slice(2)}.json`
1193
+ );
1194
+ await writeFile(configPath, JSON.stringify(SWIFT_FORMAT_CONFIG, null, 2));
1195
+ try {
1196
+ const result = await runSwiftFormat(source, configPath, options.timeoutMs ?? 8e3);
1197
+ if (result.code === 0) {
1198
+ return { formatted: result.stdout, ran: true };
1199
+ }
1200
+ if (options.strict) {
1201
+ throw new Error(`swift-format failed: ${result.stderr}`);
1202
+ }
1203
+ return {
1204
+ formatted: source,
1205
+ ran: false,
1206
+ reason: `swift-format exited ${result.code}: ${result.stderr}`
1207
+ };
1208
+ } finally {
1209
+ await unlink(configPath).catch(() => void 0);
1210
+ }
1211
+ }
1212
+ function hasSwiftFormat() {
1213
+ return new Promise((resolve3) => {
1214
+ const child = spawn("swift-format", ["--version"], { stdio: "pipe" });
1215
+ child.on("error", () => resolve3(false));
1216
+ child.on("exit", (code) => resolve3(code === 0));
1217
+ });
1218
+ }
1219
+ function runSwiftFormat(source, configPath, timeoutMs) {
1220
+ return new Promise((resolve3) => {
1221
+ const child = spawn("swift-format", ["format", "--configuration", configPath], {
1222
+ stdio: ["pipe", "pipe", "pipe"]
1223
+ });
1224
+ let stdout = "";
1225
+ let stderr = "";
1226
+ child.stdout?.on("data", (d) => stdout += d.toString());
1227
+ child.stderr?.on("data", (d) => stderr += d.toString());
1228
+ const timer = setTimeout(() => {
1229
+ child.kill("SIGKILL");
1230
+ resolve3({
1231
+ stdout,
1232
+ stderr: stderr + `
1233
+ [format] killed after ${timeoutMs}ms`,
1234
+ code: 124
1235
+ });
1236
+ }, timeoutMs);
1237
+ child.on("exit", (code) => {
1238
+ clearTimeout(timer);
1239
+ resolve3({ stdout, stderr, code: code ?? 1 });
1240
+ });
1241
+ child.stdin?.write(source);
1242
+ child.stdin?.end();
1243
+ });
1244
+ }
1245
+ var SWIFT_FORMAT_CONFIG;
1246
+ var init_format = __esm({
1247
+ "src/core/format.ts"() {
1248
+ "use strict";
1249
+ SWIFT_FORMAT_CONFIG = {
1250
+ version: 1,
1251
+ lineLength: 100,
1252
+ indentation: { spaces: 4 },
1253
+ tabWidth: 4,
1254
+ maximumBlankLines: 1,
1255
+ respectsExistingLineBreaks: true,
1256
+ lineBreakBeforeControlFlowKeywords: false,
1257
+ lineBreakBeforeEachArgument: false,
1258
+ lineBreakBeforeEachGenericRequirement: false,
1259
+ prioritizeKeepingFunctionOutputTogether: false,
1260
+ indentConditionalCompilationBlocks: true,
1261
+ lineBreakAroundMultilineExpressionChainComponents: false,
1262
+ fileScopedDeclarationPrivacy: { accessLevel: "private" },
1263
+ rules: {
1264
+ AllPublicDeclarationsHaveDocumentation: false,
1265
+ AlwaysUseLowerCamelCase: true,
1266
+ AmbiguousTrailingClosureOverload: true,
1267
+ BeginDocumentationCommentWithOneLineSummary: false,
1268
+ DoNotUseSemicolons: true,
1269
+ DontRepeatTypeInStaticProperties: true,
1270
+ FileScopedDeclarationPrivacy: true,
1271
+ FullyIndirectEnum: true,
1272
+ GroupNumericLiterals: true,
1273
+ IdentifiersMustBeASCII: true,
1274
+ NeverForceUnwrap: false,
1275
+ NeverUseForceTry: false,
1276
+ NeverUseImplicitlyUnwrappedOptionals: false,
1277
+ NoBlockComments: false,
1278
+ NoCasesWithOnlyFallthrough: true,
1279
+ NoEmptyTrailingClosureParentheses: true,
1280
+ NoLabelsInCasePatterns: true,
1281
+ NoLeadingUnderscores: false,
1282
+ NoParensAroundConditions: true,
1283
+ NoVoidReturnOnFunctionSignature: true,
1284
+ OneCasePerLine: true,
1285
+ OneVariableDeclarationPerLine: true,
1286
+ OnlyOneTrailingClosureArgument: true,
1287
+ OrderedImports: true,
1288
+ ReturnVoidInsteadOfEmptyTuple: true,
1289
+ UseEarlyExits: false,
1290
+ UseLetInEveryBoundCaseVariable: true,
1291
+ UseShorthandTypeNames: true,
1292
+ UseSingleLinePropertyGetter: true,
1293
+ UseSynthesizedInitializer: true,
1294
+ UseTripleSlashForDocumentationComments: true,
1295
+ UseWhereClausesInForLoops: false,
1296
+ ValidateDocumentationComments: false
1297
+ }
1298
+ };
1299
+ }
1300
+ });
1301
+
1166
1302
  // src/templates/index.ts
1167
1303
  function getTemplate(id) {
1168
1304
  return TEMPLATES.find((t) => t.id === id);
@@ -1525,142 +1661,6 @@ export default defineIntent({
1525
1661
  }
1526
1662
  });
1527
1663
 
1528
- // src/core/format.ts
1529
- var format_exports = {};
1530
- __export(format_exports, {
1531
- SWIFT_FORMAT_CONFIG: () => SWIFT_FORMAT_CONFIG,
1532
- formatSwift: () => formatSwift
1533
- });
1534
- import { spawn as spawn2 } from "child_process";
1535
- import { writeFile as writeFile2, unlink } from "fs/promises";
1536
- import { join as join2 } from "path";
1537
- import { tmpdir } from "os";
1538
- async function formatSwift(source, options = {}) {
1539
- const available = await hasSwiftFormat();
1540
- if (!available) {
1541
- if (options.strict) {
1542
- throw new Error(
1543
- "swift-format not found on $PATH. Install Xcode + Command Line Tools, or drop --format."
1544
- );
1545
- }
1546
- return {
1547
- formatted: source,
1548
- ran: false,
1549
- reason: "swift-format not found on $PATH"
1550
- };
1551
- }
1552
- const configPath = join2(
1553
- tmpdir(),
1554
- `axint-swift-format-${Date.now()}-${Math.random().toString(36).slice(2)}.json`
1555
- );
1556
- await writeFile2(configPath, JSON.stringify(SWIFT_FORMAT_CONFIG, null, 2));
1557
- try {
1558
- const result = await runSwiftFormat(source, configPath, options.timeoutMs ?? 8e3);
1559
- if (result.code === 0) {
1560
- return { formatted: result.stdout, ran: true };
1561
- }
1562
- if (options.strict) {
1563
- throw new Error(`swift-format failed: ${result.stderr}`);
1564
- }
1565
- return {
1566
- formatted: source,
1567
- ran: false,
1568
- reason: `swift-format exited ${result.code}: ${result.stderr}`
1569
- };
1570
- } finally {
1571
- await unlink(configPath).catch(() => void 0);
1572
- }
1573
- }
1574
- function hasSwiftFormat() {
1575
- return new Promise((resolve3) => {
1576
- const child = spawn2("swift-format", ["--version"], { stdio: "pipe" });
1577
- child.on("error", () => resolve3(false));
1578
- child.on("exit", (code) => resolve3(code === 0));
1579
- });
1580
- }
1581
- function runSwiftFormat(source, configPath, timeoutMs) {
1582
- return new Promise((resolve3) => {
1583
- const child = spawn2("swift-format", ["format", "--configuration", configPath], {
1584
- stdio: ["pipe", "pipe", "pipe"]
1585
- });
1586
- let stdout = "";
1587
- let stderr = "";
1588
- child.stdout?.on("data", (d) => stdout += d.toString());
1589
- child.stderr?.on("data", (d) => stderr += d.toString());
1590
- const timer = setTimeout(() => {
1591
- child.kill("SIGKILL");
1592
- resolve3({
1593
- stdout,
1594
- stderr: stderr + `
1595
- [format] killed after ${timeoutMs}ms`,
1596
- code: 124
1597
- });
1598
- }, timeoutMs);
1599
- child.on("exit", (code) => {
1600
- clearTimeout(timer);
1601
- resolve3({ stdout, stderr, code: code ?? 1 });
1602
- });
1603
- child.stdin?.write(source);
1604
- child.stdin?.end();
1605
- });
1606
- }
1607
- var SWIFT_FORMAT_CONFIG;
1608
- var init_format = __esm({
1609
- "src/core/format.ts"() {
1610
- "use strict";
1611
- SWIFT_FORMAT_CONFIG = {
1612
- version: 1,
1613
- lineLength: 100,
1614
- indentation: { spaces: 4 },
1615
- tabWidth: 4,
1616
- maximumBlankLines: 1,
1617
- respectsExistingLineBreaks: true,
1618
- lineBreakBeforeControlFlowKeywords: false,
1619
- lineBreakBeforeEachArgument: false,
1620
- lineBreakBeforeEachGenericRequirement: false,
1621
- prioritizeKeepingFunctionOutputTogether: false,
1622
- indentConditionalCompilationBlocks: true,
1623
- lineBreakAroundMultilineExpressionChainComponents: false,
1624
- fileScopedDeclarationPrivacy: { accessLevel: "private" },
1625
- rules: {
1626
- AllPublicDeclarationsHaveDocumentation: false,
1627
- AlwaysUseLowerCamelCase: true,
1628
- AmbiguousTrailingClosureOverload: true,
1629
- BeginDocumentationCommentWithOneLineSummary: false,
1630
- DoNotUseSemicolons: true,
1631
- DontRepeatTypeInStaticProperties: true,
1632
- FileScopedDeclarationPrivacy: true,
1633
- FullyIndirectEnum: true,
1634
- GroupNumericLiterals: true,
1635
- IdentifiersMustBeASCII: true,
1636
- NeverForceUnwrap: false,
1637
- NeverUseForceTry: false,
1638
- NeverUseImplicitlyUnwrappedOptionals: false,
1639
- NoBlockComments: false,
1640
- NoCasesWithOnlyFallthrough: true,
1641
- NoEmptyTrailingClosureParentheses: true,
1642
- NoLabelsInCasePatterns: true,
1643
- NoLeadingUnderscores: false,
1644
- NoParensAroundConditions: true,
1645
- NoVoidReturnOnFunctionSignature: true,
1646
- OneCasePerLine: true,
1647
- OneVariableDeclarationPerLine: true,
1648
- OnlyOneTrailingClosureArgument: true,
1649
- OrderedImports: true,
1650
- ReturnVoidInsteadOfEmptyTuple: true,
1651
- UseEarlyExits: false,
1652
- UseLetInEveryBoundCaseVariable: true,
1653
- UseShorthandTypeNames: true,
1654
- UseSingleLinePropertyGetter: true,
1655
- UseSynthesizedInitializer: true,
1656
- UseTripleSlashForDocumentationComments: true,
1657
- UseWhereClausesInForLoops: false,
1658
- ValidateDocumentationComments: false
1659
- }
1660
- };
1661
- }
1662
- });
1663
-
1664
1664
  // src/core/sandbox.ts
1665
1665
  var sandbox_exports = {};
1666
1666
  __export(sandbox_exports, {
@@ -2091,7 +2091,8 @@ import { fileURLToPath as fileURLToPath2 } from "url";
2091
2091
 
2092
2092
  // src/core/eject.ts
2093
2093
  init_compiler();
2094
- function ejectIntent(source, fileName, options = {}) {
2094
+ init_format();
2095
+ async function ejectIntent(source, fileName, options = {}) {
2095
2096
  const compileResult = compileSource(source, fileName, {
2096
2097
  validate: true,
2097
2098
  emitInfoPlist: true,
@@ -2104,7 +2105,11 @@ function ejectIntent(source, fileName, options = {}) {
2104
2105
  }
2105
2106
  const { ir, swiftCode, infoPlistFragment, entitlementsFragment } = compileResult.output;
2106
2107
  const outDir = options.outDir ?? ".";
2107
- const ejectedSwift = transformSwiftForEject(swiftCode, ir);
2108
+ let ejectedSwift = transformSwiftForEject(swiftCode, ir);
2109
+ if (options.format) {
2110
+ const { formatted, ran } = await formatSwift(ejectedSwift);
2111
+ if (ran) ejectedSwift = formatted;
2112
+ }
2108
2113
  const intentFileName = `${ir.name}Intent.swift`;
2109
2114
  const swiftPath = `${outDir}/${intentFileName}`;
2110
2115
  const plistPath = infoPlistFragment ? `${outDir}/${ir.name}Intent.plist.fragment.xml` : null;
@@ -2161,12 +2166,8 @@ function transformSwiftForEject(swiftCode, ir) {
2161
2166
  if (line.includes("// TODO: Implement your intent logic here.")) {
2162
2167
  result.push(` // TODO: Implement your intent logic here.`);
2163
2168
  result.push(` //`);
2164
- result.push(
2165
- ` // For more information about App Intents, see:`
2166
- );
2167
- result.push(
2168
- ` // https://developer.apple.com/documentation/appintents`
2169
- );
2169
+ result.push(` // For more information about App Intents, see:`);
2170
+ result.push(` // https://developer.apple.com/documentation/appintents`);
2170
2171
  } else {
2171
2172
  result.push(line);
2172
2173
  }
@@ -2203,10 +2204,10 @@ function generateTestFile(ir) {
2203
2204
 
2204
2205
  // src/cli/scaffold.ts
2205
2206
  init_templates();
2206
- import { mkdir, writeFile, readdir } from "fs/promises";
2207
+ import { mkdir, writeFile as writeFile2, readdir } from "fs/promises";
2207
2208
  import { existsSync } from "fs";
2208
- import { join, relative } from "path";
2209
- import { spawn } from "child_process";
2209
+ import { join as join2, relative } from "path";
2210
+ import { spawn as spawn2 } from "child_process";
2210
2211
  async function scaffoldProject(opts) {
2211
2212
  const { targetDir, projectName, template, version, install } = opts;
2212
2213
  const tpl = getTemplate(template);
@@ -2228,8 +2229,8 @@ async function scaffoldProject(opts) {
2228
2229
  }
2229
2230
  const files = [];
2230
2231
  const write = async (rel, content) => {
2231
- const abs = join(targetDir, rel);
2232
- await mkdir(join(abs, "..").replace(/[/\\][^/\\]+$/, ""), { recursive: true }).catch(
2232
+ const abs = join2(targetDir, rel);
2233
+ await mkdir(join2(abs, "..").replace(/[/\\][^/\\]+$/, ""), { recursive: true }).catch(
2233
2234
  () => void 0
2234
2235
  );
2235
2236
  const parent = abs.substring(
@@ -2239,7 +2240,7 @@ async function scaffoldProject(opts) {
2239
2240
  if (parent && parent !== abs) {
2240
2241
  await mkdir(parent, { recursive: true }).catch(() => void 0);
2241
2242
  }
2242
- await writeFile(abs, content, "utf-8");
2243
+ await writeFile2(abs, content, "utf-8");
2243
2244
  files.push(relative(targetDir, abs));
2244
2245
  };
2245
2246
  await write(
@@ -2307,7 +2308,7 @@ async function scaffoldProject(opts) {
2307
2308
  ) + "\n"
2308
2309
  );
2309
2310
  await write("README.md", scaffoldReadme(projectName, template, tpl.title, version));
2310
- await mkdir(join(targetDir, "ios", "Intents"), { recursive: true });
2311
+ await mkdir(join2(targetDir, "ios", "Intents"), { recursive: true });
2311
2312
  await write("ios/Intents/.gitkeep", "");
2312
2313
  if (install) {
2313
2314
  await runNpmInstall(targetDir);
@@ -2360,7 +2361,7 @@ _Generated by \`axint init\`_
2360
2361
  }
2361
2362
  function runNpmInstall(cwd) {
2362
2363
  return new Promise((resolve3, reject) => {
2363
- const child = spawn("npm", ["install"], { cwd, stdio: "inherit" });
2364
+ const child = spawn2("npm", ["install"], { cwd, stdio: "inherit" });
2364
2365
  child.on("exit", (code) => {
2365
2366
  if (code === 0) resolve3();
2366
2367
  else reject(new Error(`npm install exited with code ${code}`));
@@ -2664,7 +2665,7 @@ program.command("eject").description("Eject an intent to standalone Swift with n
2664
2665
  console.error(`\x1B[31merror:\x1B[0m Cannot read file: ${filePath}`);
2665
2666
  process.exit(1);
2666
2667
  }
2667
- const result = ejectIntent(source, basename(filePath), {
2668
+ const result = await ejectIntent(source, basename(filePath), {
2668
2669
  outDir: options.out,
2669
2670
  includeTests: options.includeTests,
2670
2671
  format: options.format