File size: 2,308 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
// @ts-check

/**
 * Links or unlinks `.vscode/settings-sample.jsonc` to `.vscode/settings.json`.
 *
 * Usage:
 * To link, run `node bin/set-up-vs-code.js`
 * To unlink, run `node bin/set-up-vs-code.js --unlink`
 */

const fs = require( 'fs' );
const path = require( 'path' );
const chalk = require( 'chalk' );
const yargs = require( 'yargs' );

const settingsPath = path.resolve( __dirname, '../.vscode/settings.json' );
const settingsSamplePath = path.resolve( __dirname, '../.vscode/settings-sample.jsonc' );

const stat = fs.lstatSync( settingsPath, { throwIfNoEntry: false } );

function symlinkExists() {
	return stat && stat.isSymbolicLink();
}

function fileExists() {
	return stat && stat.isFile();
}

function link() {
	if ( symlinkExists() ) {
		console.log( `Symlink for .vscode/settings.json already exists. ✅\n` );
		return;
	}
	if ( fileExists() ) {
		console.log(
			chalk.yellow(
				`Warning: Cannot create a symlink for .vscode/settings.json as the file already exists.\n`
			)
		);
		console.log(
			`You may delete .vscode/settings.json or if you wish to maintain it yourself, please copy the contents of .vscode/settings-sample.jsonc to .vscode/settings.json and make your changes.\n`
		);
		return;
	}
	try {
		fs.symlinkSync( settingsSamplePath, settingsPath, 'junction' );
		console.log( chalk.green( `Successfully created a symlink for .vscode/settings.json ✅\n` ) );
	} catch ( error ) {
		console.log( chalk.red( 'Error creating a symlink for .vscode/settings.json' ), error, `\n` );
	}
}

function unlink() {
	if ( fileExists() ) {
		console.log(
			chalk.yellow(
				`Warning: Cannot remove symlink for .vscode/settings.json as it is a file and not a symlink.\n`
			)
		);
		console.log(
			`If you do NOT want to manage .vscode/settings.json yourself, you can delete it and run\n\nyarn vscode:link\n`
		);
		return;
	}

	if ( symlinkExists() ) {
		try {
			fs.unlinkSync( settingsPath );
			console.log( chalk.green( `Removed symlink for .vscode/settings.json ✅\n` ) );
		} catch ( error ) {
			console.error(
				chalk.red( 'Failed to remove symlink for .vscode/settings.json' ),
				error,
				`\n`
			);
		}
	} else {
		console.log( `No symlink exists for .vscode/settings.json\n` );
	}
}

if ( yargs.argv.unlink ) {
	unlink();
} else {
	link();
}