lincd-cli 0.2.67 → 0.2.69

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.
@@ -13,13 +13,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.executeCommandForPackage = exports.addCapacitor = exports.addLinesToFile = exports.executeCommandForEachPackage = exports.buildUpdated = exports.publishPackage = exports.publishUpdated = exports.buildPackage = exports.register = exports.createPackage = exports.depCheck = exports.checkImports = exports.createComponent = exports.createSetComponent = exports.createShape = exports.setNameVariables = exports.createOntology = exports.getLincdPackages = exports.buildAll = exports.developPackage = exports.warn = exports.createApp = void 0;
16
- const path_1 = __importDefault(require("path"));
17
- const fs_extra_1 = __importDefault(require("fs-extra"));
18
- const utils_1 = require("./utils");
19
16
  const chalk_1 = __importDefault(require("chalk"));
20
17
  const child_process_1 = require("child_process");
21
- const get_env_vars_1 = require("env-cmd/dist/get-env-vars");
22
18
  const depcheck_1 = __importDefault(require("depcheck"));
19
+ const get_env_vars_1 = require("env-cmd/dist/get-env-vars");
20
+ const fs_extra_1 = __importDefault(require("fs-extra"));
21
+ const path_1 = __importDefault(require("path"));
22
+ const utils_1 = require("./utils");
23
+ const fs_1 = require("fs");
23
24
  var glob = require('glob');
24
25
  var variables = {};
25
26
  var open = require('open');
@@ -723,12 +724,53 @@ const createComponent = (name, basePath = process.cwd()) => __awaiter(void 0, vo
723
724
  }
724
725
  });
725
726
  exports.createComponent = createComponent;
726
- const checkImports = () => __awaiter(void 0, void 0, void 0, function* () {
727
- //read the source of all ts/tsx files in the src folder
728
- //if there is an import that imports a lincd package with /src/ in it, then warn
729
- //if there is an import that imports outside of the src folder, then warn
730
- //TODO: use dependency-cruiser for this
731
- //https://github.com/sverweij/dependency-cruiser/blob/HEAD/doc/api.md
727
+ //read the source of all ts/tsx files in the src folder
728
+ //if there is an import that imports a lincd package with /src/ in it, then warn
729
+ //if there is an import that imports outside of the src folder, then warn
730
+ const checkImports = (sourceFolder = getSourceFolder(), depth = 0, // Used to check if the import is outside of the src folder
731
+ invalidImports = new Map()) => __awaiter(void 0, void 0, void 0, function* () {
732
+ const dir = fs_extra_1.default.readdirSync(sourceFolder);
733
+ // Start checking each file in the source folder
734
+ for (const file of dir) {
735
+ const filename = path_1.default.join(sourceFolder, file);
736
+ // File is either a directory, or not a .ts(x)
737
+ // INFO: For future use - if this part fails, it could be due to user permissions
738
+ // i.e. the program not having access to check the file metadata
739
+ if (!filename.match(/\.tsx?$/)) {
740
+ if ((0, fs_1.statSync)(filename).isDirectory()) {
741
+ yield (0, exports.checkImports)(filename, depth + 1, invalidImports);
742
+ }
743
+ // Ignore all files that aren't one of the following:
744
+ // - .ts
745
+ // - .tsx
746
+ continue;
747
+ }
748
+ const allImports = yield (0, utils_1.getFileImports)(filename);
749
+ const lincdImports = allImports.filter((i) => i.includes('lincd') || i.includes('..'));
750
+ lincdImports.forEach((i) => {
751
+ if (!(0, utils_1.isValidLINCDImport)(i, depth)) {
752
+ if (!invalidImports.has(filename)) {
753
+ invalidImports.set(filename, []);
754
+ }
755
+ invalidImports.get(filename).push(i);
756
+ }
757
+ });
758
+ }
759
+ // All recursion must have finished, display any errors
760
+ if (depth === 0 && invalidImports.size > 0) {
761
+ console.warn(chalk_1.default.red('\n' + 'Invalid imports found. See fixes below:'));
762
+ console.warn(chalk_1.default.red(" - For relative imports, ensure you don't import outside of the /src/ folder"));
763
+ console.warn(chalk_1.default.red(' - For lincd imports, access the /lib/ folder instead of /src/'));
764
+ invalidImports.forEach((value, key) => {
765
+ console.info(chalk_1.default.red('\nFound in file ') + chalk_1.default.blue(key) + chalk_1.default.red(':'));
766
+ value.forEach((i) => console.warn(chalk_1.default.red("- '" + i + "'")));
767
+ });
768
+ process.exit(1);
769
+ }
770
+ else if (depth === 0 && invalidImports.size === 0) {
771
+ console.info('All imports OK');
772
+ process.exit(0);
773
+ }
732
774
  });
733
775
  exports.checkImports = checkImports;
734
776
  const depCheck = () => __awaiter(void 0, void 0, void 0, function* () {
package/lib/utils.js CHANGED
@@ -35,14 +35,69 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.getLinkedTailwindColors = exports.flatten = exports.warn = exports.debugInfo = exports.debug = exports.log = exports.needsRebuilding = exports.generateScopedName = exports.execPromise = exports.execp = exports.getGruntConfig = exports.getModulePackageJSON = exports.getLastModifiedFile = exports.getLastCommitTime = exports.getLastModifiedSourceTime = exports.getLastBuildTime = exports.getLINCDDependencies = exports.getPackageJSON = void 0;
38
+ exports.getLinkedTailwindColors = exports.flatten = exports.warn = exports.debugInfo = exports.debug = exports.log = exports.needsRebuilding = exports.generateScopedName = exports.execPromise = exports.execp = exports.getGruntConfig = exports.getModulePackageJSON = exports.getLastModifiedFile = exports.getLastCommitTime = exports.getLastModifiedSourceTime = exports.getLastBuildTime = exports.getLINCDDependencies = exports.getPackageJSON = exports.isValidLINCDImport = exports.getFileImports = void 0;
39
39
  const fs = __importStar(require("fs"));
40
40
  const path = __importStar(require("path"));
41
41
  const chalk_1 = __importDefault(require("chalk"));
42
42
  const child_process_1 = require("child_process");
43
+ const typescript_1 = __importDefault(require("typescript"));
44
+ const module_1 = require("module");
43
45
  const { findNearestPackageJson, findNearestPackageJsonSync, } = require('find-nearest-package-json');
44
46
  var glob = require('glob');
45
47
  var gruntConfig;
48
+ // Credit: https://gist.github.com/tinovyatkin/727ddbf7e7e10831a1eca9e4ff2fc32e
49
+ const tsHost = typescript_1.default.createCompilerHost({
50
+ allowJs: true,
51
+ noEmit: true,
52
+ isolatedModules: true,
53
+ resolveJsonModule: false,
54
+ moduleResolution: typescript_1.default.ModuleResolutionKind.Classic,
55
+ incremental: true,
56
+ noLib: true,
57
+ noResolve: true,
58
+ }, true);
59
+ var getFileImports = function (filePath) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const sourceFile = tsHost.getSourceFile(filePath, typescript_1.default.ScriptTarget.Latest, (msg) => {
62
+ throw new Error(`Failed to parse ${filePath}: ${msg}`);
63
+ });
64
+ if (!sourceFile)
65
+ throw ReferenceError(`Failed to find file ${filePath}`);
66
+ const importing = [];
67
+ delintNode(sourceFile);
68
+ return importing;
69
+ function delintNode(node) {
70
+ if (typescript_1.default.isImportDeclaration(node)) {
71
+ const moduleName = node.moduleSpecifier.getText().replace(/['"]/g, '');
72
+ if (!moduleName.startsWith('node:') &&
73
+ !module_1.builtinModules.includes(moduleName))
74
+ importing.push(moduleName);
75
+ }
76
+ else
77
+ typescript_1.default.forEachChild(node, delintNode);
78
+ }
79
+ });
80
+ };
81
+ exports.getFileImports = getFileImports;
82
+ /**
83
+ *
84
+ * @param importPath The import path to check
85
+ * @param curFileDepth How many folders deep the current file is (0 = src, 1 = src/foo, etc.)
86
+ * @returns
87
+ */
88
+ var isValidLINCDImport = function (importPath, curFileDepth) {
89
+ const validLincdPath = importPath.includes('lincd') && !importPath.includes('/src/');
90
+ let validRelativePath = false;
91
+ if (importPath.includes('..')) {
92
+ // '../bad/path' from 'src/file.ts' should be invalid:
93
+ // ^ should get split into ['', '/bad/path'], and the file depth is 0
94
+ // meaning that it'll be invalid.
95
+ // And this should be true for all relative imports containing '..'
96
+ validRelativePath = importPath.split('..').length - 1 <= curFileDepth;
97
+ }
98
+ return validLincdPath || validRelativePath;
99
+ };
100
+ exports.isValidLINCDImport = isValidLINCDImport;
46
101
  var getPackageJSON = function (root = process.cwd(), error = true) {
47
102
  let packagePath = path.join(root, 'package.json');
48
103
  if (fs.existsSync(packagePath)) {
@@ -172,7 +227,7 @@ const getLastBuildTime = (packagePath) => {
172
227
  };
173
228
  exports.getLastBuildTime = getLastBuildTime;
174
229
  const getLastModifiedSourceTime = (packagePath) => {
175
- return (0, exports.getLastModifiedFile)(packagePath + '/@(src|data|scss|modules)/**/*', {
230
+ return (0, exports.getLastModifiedFile)(packagePath + '/@(src|data|scss)/**/*', {
176
231
  ignore: [packagePath + '/**/*.scss.json', packagePath + '/**/*.d.ts'],
177
232
  });
178
233
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lincd-cli",
3
- "version": "0.2.67",
3
+ "version": "0.2.69",
4
4
  "description": "Command line tools for the lincd.js library",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {