sandstone-cli 2.0.8 → 2.1.1

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.
@@ -1,162 +0,0 @@
1
- import { SemVer } from 'semver';
2
- import fs from 'fs-extra';
3
- import path from 'path';
4
- import chalk from 'chalk-template';
5
- import util from 'util';
6
- import * as child from 'child_process';
7
- import { nanoid } from 'nanoid';
8
- import { confirm, select, input } from '@inquirer/prompts';
9
- import { capitalize, getWorldsList, hasBun, hasPnpm, hasYarn } from '../utils.js';
10
- function toJson(obj, pretty = false) {
11
- return util.inspect(obj, {
12
- depth: Number(Infinity),
13
- colors: false,
14
- breakLength: Number(Infinity),
15
- compact: !pretty,
16
- maxArrayLength: Number(Infinity),
17
- });
18
- }
19
- export async function createCommand(_project, opts) {
20
- const projectPath = path.resolve(_project);
21
- const projectName = path.basename(projectPath);
22
- const projectType = (await confirm({
23
- message: 'Whether your project will be a library for use in other Sandstone projects >',
24
- default: false,
25
- })) === true ? 'library' : 'pack';
26
- const sv = (v) => new SemVer(v);
27
- const versions = [[sv('1.0.0-beta.1'), sv('2.0.8')]];
28
- const version = await select({
29
- message: 'Which version of Sandstone do you want to use? These are the only supported versions for new projects.',
30
- choices: versions.map((v) => {
31
- const { prerelease, major, minor } = v[0];
32
- const release = `${major}.${minor}`;
33
- return {
34
- name: prerelease.length === 0 ?
35
- `Release Version ${release}` :
36
- `${capitalize(prerelease[0])} Version ${prerelease[1]} for release ${release}`,
37
- value: v,
38
- short: v[0].toString(),
39
- };
40
- }),
41
- default: versions[0],
42
- });
43
- let packName = projectName;
44
- let namespace = projectName.replace(RegExp(/ /g), '_');
45
- if (projectType === 'pack') {
46
- packName = (await input({
47
- message: 'Name of your output pack(s) (can be changed later) >',
48
- default: projectName,
49
- }));
50
- namespace = (await input({
51
- message: 'Default namespace (can be changed later) >',
52
- default: namespace,
53
- }));
54
- }
55
- else {
56
- packName += '-testing';
57
- namespace += '_test';
58
- }
59
- // Find the save directory
60
- const saveOptions = {};
61
- if (version[0].major === 1) {
62
- if (opts.clientPath) {
63
- saveOptions.clientPath = opts.clientPath;
64
- }
65
- if (opts.root) {
66
- saveOptions.root = true;
67
- }
68
- else if (opts.world) {
69
- saveOptions.world = opts.world;
70
- }
71
- else if (opts.serverPath) {
72
- saveOptions.serverPath = opts.serverPath;
73
- }
74
- else { // TODO: Add support for ssh
75
- // User didn't specify a way to save the file. Ask them.
76
- const saveChoice = await select({
77
- message: 'Where do you want your pack(s) to be exported to (can be changed later)?',
78
- choices: [{
79
- name: 'In the root client (.minecraft/datapacks & .minecraft/resourcepacks) folder(s)',
80
- value: 'root',
81
- short: 'Client folder',
82
- }, {
83
- name: 'In a world',
84
- value: 'world',
85
- short: 'World',
86
- }, {
87
- name: 'In a server',
88
- value: 'server-path',
89
- short: 'Server path',
90
- }, {
91
- name: 'N/A',
92
- value: 'none',
93
- short: 'None',
94
- }],
95
- });
96
- switch (saveChoice) {
97
- case 'root':
98
- saveOptions.root = true;
99
- break;
100
- case 'world':
101
- const world = await select({
102
- message: 'What world do you want to save the packs in? >',
103
- choices: getWorldsList(saveOptions.clientPath),
104
- });
105
- saveOptions.world = world;
106
- break;
107
- case 'server-path':
108
- const serverPath = await input({
109
- message: 'Where is the server to save the packs in? Relative paths are accepted. >',
110
- });
111
- saveOptions.serverPath = serverPath;
112
- break;
113
- case 'none': break;
114
- }
115
- }
116
- }
117
- let packageManager = 'npm';
118
- const yarn = hasYarn();
119
- const pnpm = hasPnpm();
120
- const bun = hasBun();
121
- if (yarn || pnpm || bun) {
122
- const choices = ['npm'];
123
- if (yarn)
124
- choices.unshift('yarn');
125
- if (pnpm)
126
- choices.unshift('pnpm');
127
- if (bun)
128
- choices.unshift('bun');
129
- packageManager = (await select({
130
- message: 'What package manager do you want to use? (For now you have to use Bun) >',
131
- choices: choices
132
- }));
133
- }
134
- fs.mkdirSync(projectPath);
135
- // Create project & install dependencies
136
- console.log(chalk `Installing {rgb(229, 193, 0) sandstone@${version[0]}}, {rgb(229, 193, 0) sandstone-cli@${version[1]}} and {cyan typescript} using {cyan ${packageManager}}.`);
137
- const exec = (cmd) => child.execSync(cmd, { cwd: projectPath });
138
- exec('git clone https://github.com/sandstone-mc/sandstone-template.git .');
139
- exec(`git checkout ${projectType}-${version[0]}`);
140
- await fs.rm('.git', { force: true, recursive: true });
141
- exec(`${packageManager} install`);
142
- const configPath = path.join(projectPath, `${projectType === 'library' ? 'test/' : ''}sandstone.config.ts`);
143
- // Merge with the config values
144
- let templateConfig = await fs.readFile(configPath, 'utf8');
145
- templateConfig = templateConfig.replace('packUid: \'kZZpDK67\'', `packUid: ${toJson(nanoid(8))}`);
146
- templateConfig = templateConfig.replace('name: \'template\'', `name: ${toJson(packName)}`);
147
- templateConfig = templateConfig.replace('namespace: \'default\'', `namespace: ${toJson(namespace)}`);
148
- // TODO: packFormat
149
- const optsJson = toJson(Object.fromEntries(Object.entries(saveOptions).filter(([_, value]) => value !== undefined)));
150
- if (optsJson !== '{}') {
151
- templateConfig = templateConfig.replace('saveOptions: {}', `saveOptions: ${optsJson}`);
152
- }
153
- // Rewrite config
154
- fs.writeFileSync(configPath, templateConfig);
155
- const prefix = packageManager === 'npm' ? 'npm run' : packageManager;
156
- console.log(chalk `{green Success!} Created "${projectName}" at "${projectPath}"`);
157
- console.log('Inside that directory, you can run several commands:\n');
158
- console.log(chalk ` {cyan ${prefix} dev:build}:\n Builds the packs. {cyan ⛏}\n`);
159
- console.log(chalk ` {cyan ${prefix} dev:watch}:\n Builds the packs, and rebuilds on each file change. {cyan ⛏}\n`);
160
- console.log('We suggest that you begin by typing:\n');
161
- console.log(chalk ` {cyan cd} ${projectName}\n {cyan ${prefix} dev:watch}`);
162
- }
@@ -1,4 +0,0 @@
1
- export declare function installNativeCommand(_libraries: string[]): Promise<void>;
2
- export declare function installVanillaCommand(_libraries: string[]): Promise<void>;
3
- export declare function uninstallVanillaCommand(_libraries: string[]): Promise<void>;
4
- export declare function refreshCommand(): Promise<void>;
@@ -1,246 +0,0 @@
1
- import fs from 'fs-extra';
2
- import path from 'path';
3
- import { exec } from 'child_process';
4
- import { buildCommand } from './build.js';
5
- import { checkbox } from '@inquirer/prompts';
6
- const _fetch = import('node-fetch');
7
- export async function installNativeCommand(_libraries) {
8
- let libraries = _libraries.map((lib) => [lib, false]);
9
- let count = libraries.length || 0;
10
- const fetch = (await _fetch).default;
11
- const manifest = await (await fetch('https://raw.githubusercontent.com/sandstone-mc/sandstone-libraries/main/manifest.json')).json();
12
- const search = async () => {
13
- const selected = await checkbox({
14
- message: 'Which libraries to add?',
15
- choices: manifest.libraries.map((library) => ({
16
- name: library.name,
17
- value: library.package,
18
- })),
19
- });
20
- if (selected && selected.length !== 0) {
21
- libraries.push(...selected.map((lib) => [lib, true]));
22
- count += selected.length;
23
- }
24
- };
25
- if (count === 0) {
26
- await search();
27
- }
28
- if (count > 0) {
29
- let adding = false;
30
- for await (const [library, searched] of libraries) {
31
- if (searched) {
32
- if (!adding)
33
- adding = [];
34
- adding.push(library);
35
- }
36
- else {
37
- let exists = manifest.libraries.find((lib) => lib.name === library);
38
- if (exists) {
39
- if (!adding)
40
- adding = [];
41
- adding.push(exists.package);
42
- }
43
- else {
44
- count--;
45
- console.log(`${library} doesn't exist!`);
46
- }
47
- }
48
- }
49
- if (adding) {
50
- console.log(`Installing ${adding.join(', ')}...`);
51
- const pnpm = await fs.exists(path.resolve('./pnpm-lock.yaml'));
52
- const yarn = await fs.exists(path.resolve('./yarn.lock'));
53
- const npm = await fs.exists(path.resolve('./package-lock.json'));
54
- if (pnpm) {
55
- exec(`pnpm i ${adding.join(' ')}`);
56
- }
57
- else if (yarn) {
58
- exec(`yarn add ${adding.join(' ')}`);
59
- }
60
- else if (npm) {
61
- exec(`npm i ${adding.join(' ')}`);
62
- }
63
- else {
64
- console.error('error: no package manager lockfile');
65
- }
66
- }
67
- }
68
- }
69
- export async function installVanillaCommand(_libraries) {
70
- let libraries = _libraries.map((lib) => [lib, false]);
71
- let count = libraries.length || 0;
72
- let manifest = false;
73
- try {
74
- manifest = JSON.parse(await fs.readFile(path.resolve('./resources/smithed.json'), 'utf-8'));
75
- }
76
- catch (e) { }
77
- const fetch = (await _fetch).default;
78
- const base = 'https://api.smithed.dev/v2';
79
- const search = async (term) => {
80
- const options = [];
81
- const optionColumn = [0, 0, 0];
82
- const scopes = ['data.display.description', 'meta.rawId', 'meta.stats.downloads.total', 'owner.displayName'].map((scope) => `scope=${scope}`).join('&');
83
- for await (const { id, displayName, meta, data, owner } of await (await fetch(`${base}/packs?category=Library&sort=downloads&${scopes}${term ? `&search=${encodeURIComponent(term)}` : ''}`)).json()) {
84
- const option = {
85
- id: meta.rawId,
86
- name: displayName,
87
- owner: owner.displayName,
88
- downloads: `${meta.stats.downloads.total}`,
89
- description: data.display.description,
90
- };
91
- if (!manifest || !(manifest[id] || manifest[meta.rawId])) {
92
- if (option.name.length > optionColumn[0])
93
- optionColumn[0] = option.name.length;
94
- if (option.owner.length > optionColumn[1])
95
- optionColumn[1] = option.owner.length;
96
- if (option.downloads.length > optionColumn[2])
97
- optionColumn[2] = option.downloads.length;
98
- options.push(option);
99
- }
100
- }
101
- const space = (index, option) => {
102
- const length = optionColumn[index] - option.length;
103
- let _space = '';
104
- for (let i = 0; i < length; i++) {
105
- _space += ' ';
106
- }
107
- return `${_space} - `;
108
- };
109
- if (options.length === 0) {
110
- console.log('No results found!');
111
- }
112
- else {
113
- const selected = await checkbox({
114
- message: 'Which libraries to add?',
115
- choices: options.map((option) => ({
116
- name: `${option.name}${space(0, option.name)}by ${option.owner}${space(1, option.owner)}${option.downloads} downloads${space(2, option.downloads)}${option.description}`,
117
- value: [option.id, true],
118
- })),
119
- });
120
- if (selected && selected.length !== 0) {
121
- libraries.push(...selected);
122
- count += selected.length;
123
- return true;
124
- }
125
- }
126
- return false;
127
- };
128
- if (count === 0) {
129
- await search();
130
- }
131
- if (count > 0) {
132
- let adding = false;
133
- for await (const [library, searched] of libraries) {
134
- const version = library.includes('@') ? library.split('@')[1] : 'latest';
135
- if (searched) {
136
- if (!adding)
137
- adding = [];
138
- adding.push([library, version]);
139
- }
140
- if (!manifest || !(manifest[library] || manifest[library] === version)) {
141
- let exists = false;
142
- try {
143
- /* @ts-ignore */
144
- exists = (await (await fetch(`${base}/packs/${library}/meta`)).json()).statusCode !== 404;
145
- }
146
- catch (e) { }
147
- if (exists) {
148
- if (!adding)
149
- adding = [];
150
- adding.push([library, version]);
151
- }
152
- else {
153
- count--;
154
- console.log(`${library} doesn't exist! Searching...`);
155
- if (await search(library)) {
156
- if (!adding)
157
- adding = [];
158
- }
159
- }
160
- }
161
- else {
162
- count--;
163
- }
164
- }
165
- if (adding) {
166
- await buildCommand({
167
- path: './',
168
- dependencies: adding
169
- });
170
- }
171
- }
172
- console.log(`${count} libraries added`);
173
- }
174
- export async function uninstallVanillaCommand(_libraries) {
175
- const libraries = _libraries || [];
176
- let count = libraries.length || 0;
177
- let manifestPath = path.resolve('./resources/smithed.json');
178
- let manifest = false;
179
- let lockFilePath = path.resolve('./resources/cache/lock-smithed.json');
180
- let lockFile = false;
181
- try {
182
- manifest = JSON.parse(await fs.readFile(manifestPath, 'utf-8'));
183
- lockFile = JSON.parse(await fs.readFile(lockFilePath, 'utf-8'));
184
- }
185
- catch (e) { }
186
- if (manifest) {
187
- if (count === 0) {
188
- const selected = await checkbox({
189
- message: 'Which libraries to remove?',
190
- choices: Object.entries(manifest).map(([name]) => ({
191
- short: name,
192
- value: name,
193
- })),
194
- });
195
- if (selected && selected.length !== 0) {
196
- count = selected.length;
197
- libraries.push(...selected);
198
- }
199
- }
200
- if (count > 0) {
201
- for await (const library of libraries) {
202
- if (manifest[library]) {
203
- delete manifest[library];
204
- await fs.remove(path.resolve(`./resources/cache/smithed/${library}`));
205
- if (lockFile) {
206
- delete lockFile[library];
207
- }
208
- }
209
- else {
210
- count--;
211
- }
212
- }
213
- await fs.writeFile(manifestPath, JSON.stringify(manifest));
214
- if (lockFile) {
215
- await fs.writeFile(lockFilePath, JSON.stringify(lockFile));
216
- }
217
- }
218
- }
219
- else {
220
- console.error('error: no dependency manifest');
221
- }
222
- if (count === 0 && manifest) {
223
- console.log('Libraries not found, installed libraries:');
224
- Object.entries(manifest).forEach(([lib]) => console.log(lib));
225
- }
226
- console.log(`${count} libraries removed`);
227
- }
228
- export async function refreshCommand() {
229
- let lockFilePath = path.resolve('./resources/cache/lock-smithed.json');
230
- let lockFile = false;
231
- try {
232
- lockFile = JSON.parse(await fs.readFile(lockFilePath, 'utf-8'));
233
- }
234
- catch (e) { }
235
- if (lockFile) {
236
- console.log('Refreshing libraries...');
237
- await fs.remove(path.resolve('./resources/cache/smithed'));
238
- await fs.writeFile(lockFilePath, '{}');
239
- await buildCommand({
240
- path: './',
241
- });
242
- }
243
- else {
244
- console.log('No libraries to refresh');
245
- }
246
- }
@@ -1,4 +0,0 @@
1
- export { buildCommand } from './build.js';
2
- export { createCommand } from './create.js';
3
- export { installNativeCommand, installVanillaCommand, uninstallVanillaCommand, refreshCommand } from './dependency.js';
4
- export { watchCommand, type WatchOptions } from './watch.js';
@@ -1,4 +0,0 @@
1
- export { buildCommand } from './build.js';
2
- export { createCommand } from './create.js';
3
- export { installNativeCommand, installVanillaCommand, uninstallVanillaCommand, refreshCommand } from './dependency.js';
4
- export { watchCommand } from './watch.js';
@@ -1,6 +0,0 @@
1
- import { type BuildOptions } from './build.js';
2
- export interface WatchOptions extends BuildOptions {
3
- manual?: boolean;
4
- library?: boolean;
5
- }
6
- export declare function watchCommand(opts: WatchOptions): Promise<void>;