@bananacool467/pt 0.1.0-beta.2 → 0.1.0-beta.4
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/README.md +13 -0
- package/glob-ignore.js +60 -0
- package/package.json +8 -3
- package/pt.js +58 -93
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
1
|
# Package Testr (PT)
|
|
2
2
|
|
|
3
3
|
Test your NPM packages before publishing...
|
|
4
|
+
|
|
5
|
+
## How to use
|
|
6
|
+
|
|
7
|
+
Run into your project's root for NPM and install it:
|
|
8
|
+
```bash
|
|
9
|
+
npm @bananacool467/pt
|
|
10
|
+
```
|
|
11
|
+
It may create a command named `pt` if scripts is allowed or something like that.
|
|
12
|
+
|
|
13
|
+
Then literally just build the package:
|
|
14
|
+
```bash
|
|
15
|
+
pt build
|
|
16
|
+
```
|
package/glob-ignore.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export function compilePattern(rawPattern) {
|
|
2
|
+
let pattern = rawPattern;
|
|
3
|
+
let negate = false;
|
|
4
|
+
|
|
5
|
+
if (pattern.startsWith('\\!') || pattern.startsWith('\\#')) {
|
|
6
|
+
pattern = pattern.slice(1);
|
|
7
|
+
} else if (pattern.startsWith('!')) {
|
|
8
|
+
negate = true;
|
|
9
|
+
pattern = pattern.slice(1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let dirOnly = false;
|
|
13
|
+
if (pattern.endsWith('/')) {
|
|
14
|
+
dirOnly = true;
|
|
15
|
+
pattern = pattern.slice(0, -1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let anchored = false;
|
|
19
|
+
if (pattern.startsWith('/')) {
|
|
20
|
+
anchored = true;
|
|
21
|
+
pattern = pattern.slice(1);
|
|
22
|
+
} else if (pattern.includes('/')) {
|
|
23
|
+
anchored = true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let re = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
27
|
+
|
|
28
|
+
re = re
|
|
29
|
+
.replace(/\*\*\//g, '\u0000DSTAR_SLASH\u0000')
|
|
30
|
+
.replace(/\*\*/g, '\u0000DSTAR\u0000')
|
|
31
|
+
.replace(/\*/g, '[^/]*')
|
|
32
|
+
.replace(/\?/g, '[^/]')
|
|
33
|
+
.replace(/\u0000DSTAR_SLASH\u0000/g, '(?:.*/)?')
|
|
34
|
+
.replace(/\u0000DSTAR\u0000/g, '.*');
|
|
35
|
+
|
|
36
|
+
const body = anchored ? `^${re}` : `(?:^|.*/)${re}`;
|
|
37
|
+
const full = dirOnly ? `${body}(?:/.*)?$` : `${body}$`;
|
|
38
|
+
|
|
39
|
+
return { regex: new RegExp(full), negate, dirOnly };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function parsePatterns(rawText) {
|
|
43
|
+
return rawText
|
|
44
|
+
.split('\n')
|
|
45
|
+
.map(l => l.trim())
|
|
46
|
+
.filter(l => l !== '' && !l.startsWith('#'))
|
|
47
|
+
.map(compilePattern);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isIgnored(filePath, compiledPatterns) {
|
|
51
|
+
let ignored = false;
|
|
52
|
+
for (const { regex, negate } of compiledPatterns) {
|
|
53
|
+
if (regex.test(filePath)) {
|
|
54
|
+
ignored = !negate;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return ignored;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//module.exports = { compilePattern, parsePatterns, isIgnored };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bananacool467/pt",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.4",
|
|
4
4
|
"description": "Test your NPM packages without publishing them.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"test",
|
|
@@ -10,11 +10,16 @@
|
|
|
10
10
|
"author": "bananacool467",
|
|
11
11
|
"type": "module",
|
|
12
12
|
"main": "pt.js",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/bananakitssu/pt.git"
|
|
16
|
+
},
|
|
13
17
|
"bin": {
|
|
14
|
-
"pt": "
|
|
18
|
+
"pt": "pt.js"
|
|
15
19
|
},
|
|
16
20
|
"scripts": {
|
|
17
|
-
"test": "npm link && pt
|
|
21
|
+
"test": "npm link && pt build && pt link",
|
|
22
|
+
"prepublishOnly": "echo -e \"test\n\nnewline\""
|
|
18
23
|
},
|
|
19
24
|
"engines": {
|
|
20
25
|
"node": ">=18.0.0"
|
package/pt.js
CHANGED
|
@@ -4,6 +4,7 @@ import * as fs from 'node:fs';
|
|
|
4
4
|
import * as path from 'node:path';
|
|
5
5
|
import * as child_process from 'node:child_process';
|
|
6
6
|
import { fileURLToPath } from 'url';
|
|
7
|
+
import { parsePatterns, isIgnored } from './glob-ignore.js';
|
|
7
8
|
|
|
8
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
9
10
|
const __dirname = path.dirname(__filename);
|
|
@@ -17,7 +18,7 @@ async function __loadPackage () {
|
|
|
17
18
|
const data = JSON.parse(rawData);
|
|
18
19
|
return data;
|
|
19
20
|
} catch (error) {
|
|
20
|
-
console.error("Error reading file:", error);
|
|
21
|
+
console.error("Error reading file: ", error);
|
|
21
22
|
return {};
|
|
22
23
|
}
|
|
23
24
|
}
|
|
@@ -35,7 +36,7 @@ async function __generatePackageTestName () {
|
|
|
35
36
|
__new = `pack${__new}`
|
|
36
37
|
return __new;
|
|
37
38
|
} else {
|
|
38
|
-
let __new = `pack
|
|
39
|
+
let __new = `pack:${__name}`;
|
|
39
40
|
return __new;
|
|
40
41
|
}
|
|
41
42
|
}
|
|
@@ -62,114 +63,70 @@ async function __parseIgnore (name) {
|
|
|
62
63
|
'v': name
|
|
63
64
|
}
|
|
64
65
|
];
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
let filteredData = [];
|
|
69
|
-
for (const ignored of data) {
|
|
70
|
-
if (ignored == '')
|
|
71
|
-
continue;
|
|
72
|
-
filteredData.push(ignored);
|
|
73
|
-
}
|
|
74
|
-
let parsed = [
|
|
75
|
-
{
|
|
76
|
-
'dI': false,
|
|
77
|
-
'iD': true,
|
|
78
|
-
'v': '.git'
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
'dI': false,
|
|
82
|
-
'iD': true,
|
|
83
|
-
'v': name
|
|
84
|
-
}
|
|
85
|
-
];
|
|
86
|
-
for (const item of filteredData) {
|
|
87
|
-
let __dontIgnore = false;
|
|
88
|
-
let __isDir = false;
|
|
89
|
-
if (item.startsWith("!"))
|
|
90
|
-
__dontIgnore = true;
|
|
91
|
-
let __ = item.replace("!", "");
|
|
92
|
-
let __resolved3 = path.resolve(__);
|
|
93
|
-
if (fs.existsSync(__resolved3)) {
|
|
94
|
-
const stats = fs.statSync(__resolved3);
|
|
95
|
-
__isDir = stats.isDirectory();
|
|
96
|
-
}
|
|
97
|
-
parsed.push({
|
|
98
|
-
"dI": __dontIgnore,
|
|
99
|
-
"iD": __isDir,
|
|
100
|
-
"v": __
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
return parsed;
|
|
104
|
-
} catch (error) {
|
|
105
|
-
console.error("Error reading file:", error);
|
|
106
|
-
return [
|
|
107
|
-
{
|
|
108
|
-
'dI': false,
|
|
109
|
-
'iD': true,
|
|
110
|
-
'v': '.git'
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
'dI': false,
|
|
114
|
-
'iD': true,
|
|
115
|
-
'v': name
|
|
116
|
-
}
|
|
117
|
-
];
|
|
118
|
-
}
|
|
66
|
+
const patterns = parsePatterns(fs.readFileSync(__1 ? __resolved : __resolved2, 'utf8'));
|
|
67
|
+
patterns.push(...parsePatterns('.git\n' + name));
|
|
68
|
+
return patterns;
|
|
119
69
|
}
|
|
120
70
|
|
|
121
71
|
async function build () {
|
|
122
72
|
const __testName = await __generatePackageTestName();
|
|
123
73
|
console.log(` [|] New testing folder: \`${__testName}\``);
|
|
124
|
-
const __testPath = path.join(
|
|
74
|
+
const __testPath = path.join(process.cwd(), __testName);
|
|
125
75
|
fs.mkdirSync(__testPath, { recursive: true });
|
|
126
76
|
const parsedIgnore = await __parseIgnore(__testName);
|
|
77
|
+
const patterns = parsedIgnore;
|
|
127
78
|
let files = 0;
|
|
128
|
-
let dirs = 0;
|
|
129
|
-
for (const item of
|
|
130
|
-
|
|
131
|
-
dirs++;
|
|
132
|
-
} else if (!item.iD && !item.dI) {
|
|
133
|
-
files++;
|
|
134
|
-
}
|
|
79
|
+
//let dirs = 0;
|
|
80
|
+
for (const item of patterns) {
|
|
81
|
+
files++;
|
|
135
82
|
}
|
|
136
|
-
const s = files == 1 ? "
|
|
137
|
-
const s2 = dirs == 1 ? "directory" : "directories";
|
|
138
|
-
console.log(` [|] Ignoring ${files} ${s} and ${dirs} ${s2}`);
|
|
83
|
+
const s = files == 1 ? "item" : "items";
|
|
84
|
+
//const s2 = dirs == 1 ? "directory" : "directories";
|
|
85
|
+
console.log(` [|] Ignoring ${files} ${s}`); // and ${dirs} ${s2}`);
|
|
139
86
|
const pkg = await __loadPackage();
|
|
140
|
-
const prepublishOnly = pkg.scripts
|
|
141
|
-
const prepare = pkg.scripts
|
|
87
|
+
const prepublishOnly = pkg.scripts?.prepublishOnly;
|
|
88
|
+
const prepare = pkg.scripts?.prepare;
|
|
142
89
|
const script = prepublishOnly ? prepublishOnly : (prepare ? prepare : null);
|
|
143
90
|
if (script) {
|
|
144
|
-
console.log(` [|] Running
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
91
|
+
console.log(` [|] Running script for \`prepublishOnly\`/\`prepare\` before copying files...`);
|
|
92
|
+
try {
|
|
93
|
+
const stdout = child_process.execSync(script, { stdio: ['pipe', 'pipe', 'pipe'], encoding: 'utf8' });
|
|
94
|
+
if (stdout && stdout.trim()) {
|
|
95
|
+
const __stdout = stdout.trim().split("\n");
|
|
96
|
+
for (const __stdout_item of __stdout)
|
|
97
|
+
console.log(` [i] ${__stdout_item.trim()}`);
|
|
149
98
|
}
|
|
150
|
-
|
|
151
|
-
|
|
99
|
+
} catch (error) {
|
|
100
|
+
const errMessage = error.stderr ? error.stderr.trim() : error.message;
|
|
101
|
+
throw new Error(` [X] Failed to run script. Got: \n${errMessage}\n Stopping...`);
|
|
102
|
+
}
|
|
152
103
|
}
|
|
153
104
|
const __files = fs.readdirSync('.', { recursive: true });
|
|
105
|
+
const ignoredDirs = [];
|
|
106
|
+
let i = 0;
|
|
107
|
+
if (fs.existsSync(__testPath)) {
|
|
108
|
+
fs.rmSync(__testPath, { recursive: true, force: true });
|
|
109
|
+
console.log(" [-] Deleted cached package");
|
|
110
|
+
}
|
|
111
|
+
console.log(" [|] Copying...");
|
|
154
112
|
for (const file of __files) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (skip) {
|
|
113
|
+
const posixFile = file.split(path.sep).join('/');
|
|
114
|
+
|
|
115
|
+
if (ignoredDirs.some(dir => posixFile.startsWith(dir + '/'))) continue;
|
|
116
|
+
|
|
117
|
+
if (isIgnored(posixFile, patterns)) {
|
|
118
|
+
const full = path.join(process.cwd(), file);
|
|
119
|
+
let isDir = false;
|
|
120
|
+
try { isDir = fs.statSync(full).isDirectory(); } catch {}
|
|
121
|
+
if (isDir) ignoredDirs.push(posixFile);
|
|
168
122
|
continue;
|
|
169
|
-
} else {
|
|
170
|
-
fs.cpSync(file, path.join(__testPath, file), { recursive: true/*, filter: (src) => { return !src.includes('.git') && !src.includes(__testName); }*/ });
|
|
171
123
|
}
|
|
124
|
+
|
|
125
|
+
fs.cpSync(file, path.join(__testPath, file), { recursive: true });
|
|
126
|
+
console.log(" [|] " + file);
|
|
127
|
+
i++;
|
|
172
128
|
}
|
|
129
|
+
console.log(` [+] Successfully built package! Copied ${i} files.`);
|
|
173
130
|
}
|
|
174
131
|
|
|
175
132
|
async function main () {
|
|
@@ -179,8 +136,16 @@ async function main () {
|
|
|
179
136
|
throw new Error("The package.json was not found. Try moving to the root directory of your project or run: npm init -y");
|
|
180
137
|
if (command)
|
|
181
138
|
console.log(" [|] Checking for `package.json...`");
|
|
182
|
-
if (command == "build")
|
|
183
|
-
build()
|
|
139
|
+
if (command == "build") {
|
|
140
|
+
build().catch(err => {
|
|
141
|
+
console.error(" [X] Build failed, error:");
|
|
142
|
+
const lines = err.message ? err.message.split('\n') : err.split('\n');
|
|
143
|
+
for (const line of lines) {
|
|
144
|
+
console.error(" " + line);
|
|
145
|
+
}
|
|
146
|
+
process.exit(1);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
184
149
|
if (!command)
|
|
185
150
|
console.log("Usage:\n build - Builds the NPM package");
|
|
186
151
|
}
|