diff --git a/app.vue b/app.vue index 1c6fa79..2b57b65 100644 --- a/app.vue +++ b/app.vue @@ -55,7 +55,7 @@ import {posixTests, win32Tests} from "~/lib/nodePathTestExamples.mjs"; // import {posixTests, win32Tests} from "##/lib/nodePathTestExamples.mjs"; // import {pathPosixToWin32, pathWin32ToPosix,win32ToWin32JS,win32ToWin32Slash,win32ToWin32WSL2} from "##/lib/dist/index.mjs"; -import {pathPosixToWin32, pathWin32ToPosix,win32ToWin32JS,win32ToWin32Slash,win32ToWin32WSL2} from "~/lib/dist/index.mjs"; +import {pathPosixToWin32, pathWin32ToPosix,win32ToWin32JS,win32ToWin32Slash,win32ToWin32WSL2} from "##/lib/dist/index.mjs"; export default { name:'app', mounted(){ diff --git a/dev/node-fs-utils-dev/SpawnCmd.mjs b/dev/node-fs-utils-dev/SpawnCmd.mjs index b36b46d..bd828a5 100644 --- a/dev/node-fs-utils-dev/SpawnCmd.mjs +++ b/dev/node-fs-utils-dev/SpawnCmd.mjs @@ -11,23 +11,26 @@ import { spawn } from 'node:child_process' import {stdout,stderr} from 'node:process' /** might be better just to use buffer like they are */ -export function spawnExecCmd(cmd,...args){ +export function spawnExecCmd(cmd,args=[],opts={}){ + const actual = {shell:true,...opts} return new Promise((resolve, reject) => { let code, stdouts = [], stderrs = [], signal; - let spawnCmd = spawn(cmd,[],{shell:true}); + // let spawnCmd = spawn(cmd,[],{shell:true}); + let spawnCmd = spawn(cmd,args,actual); //i think on error need to double check spawnCmd.on('error',(err)=>{ reject(err); }); + spawnCmd.stdout.pipe(stdout);//works spawnCmd.stdout.on('data', (data) => { // console.log(`stdout: ${data}`); stdouts.push(data); }); - spawnCmd.stdout.pipe(stdout);//works /* doesnt work pipe on chai, but two on's work n*/ + spawnCmd.stderr.pipe(stderr) spawnCmd.stderr.on('data', (data) => { // console.error(`stderr: ${data}`); stderrs.push(data); diff --git a/dev/node-fs-utils-dev/readline-debug.mjs b/dev/node-fs-utils-dev/readline-debug.mjs new file mode 100644 index 0000000..fcef59b --- /dev/null +++ b/dev/node-fs-utils-dev/readline-debug.mjs @@ -0,0 +1,14 @@ +import readline from 'node:readline' +import { stdin as input, stdout as output } from 'node:process'; +import {promisify} from 'node:util'; + +let rl = readline.createInterface({ + input, + output, + prompt:'hit enter to continue' +}); +const question = promisify(rl.question).bind(rl); +const tmp = await question('What is your name?') +console.log('tmp is ',tmp); + +rl.close(); diff --git a/dev/node-fs-utils-dev/vite-press-path-utils.test.mjs b/dev/node-fs-utils-dev/vite-press-path-utils.test.mjs new file mode 100644 index 0000000..c8f710f --- /dev/null +++ b/dev/node-fs-utils-dev/vite-press-path-utils.test.mjs @@ -0,0 +1,122 @@ +/** + * Also in material design 3.. git + */ + +/** + yarn add mocha -D + + package.json + "imports": { + "##/*": { + "default": "./*" + }, + }, + "type": "module", + + jsconfig.json + { + "compilerOptions": { + "baseUrl": ".", + "paths": { + "##/*": ["./*"] + } + }, + "exclude": ["node_modules", ".nuxt", "dist"] +} + + + + */ +// import { createRequire } from 'module'; +// const require = createRequire(import.meta.url); +// const assert = require('assert'); +// const {describe,it} = require('mocha'); +import assert from 'node:assert'; +import { describe, it} from 'mocha'; +/* +1. +yarn add mocha @babel/polyfill @babel/register @babel/preset-env babel-plugin-module-resolver --dev +yarn add @babel/core --dev +2. +-r @babel/register -r babel-plugin-module-resolver + +3. +.babelrc +{ + + "presets": ["@babel/preset-env"], + "plugins": [ + ["module-resolver", { + "root": ["./src"], + "alias": { + "test": "./test", + "underscore": "lodash", + + "~": "./" + } + }] + ] + +} +test specific timeout +this.timeout(500);//500ms +*/ +/** + * Should put this somewhere safe + * todo filepath needs to be initialized as well... + * @param fileName .json + * @param data will automatically be changed + */ +import fs from 'node:fs'; +function writeToFile(fileName,data,space=2){ + const sFileName = /\./.test(fileName) ? fileName : fileName + '.json'; + const filePath = `dev/pbs/test/${sFileName}` + fs.writeFileSync(filePath, + typeof data === 'string' ? data :JSON.stringify(data,null,+space) + ); +} +import {relative,resolve,join} from "node:path/posix"; +import {fileURLToPath} from "url"; + +describe('vite-press-path-utils.test.mjs', function(){ + let viteConfigFile; + /** @type {'docs/.vitepress' | string} - .vitepress config */ + viteConfigFile = 'docs/.vitepress'; + + const testMatrix = [ + //"" is cwd + [resolve(""),"../.."],//,cwd, new URL('./',import.meta.url).pathname??? + [resolve("./src"),"../../src"],//cwd/src, cant be ../src + [resolve("src"),"../../src"],//cwd/src, same as above. left is okay to repeat + [resolve("./docs/src"),"../src"], + // [resolve("./docs"),"../"],//'../' also works and is more clear + [resolve("./docs"),".."],//'../' also works and is more clear + ] + it('first try', function(){ + for (let i = 0; i < testMatrix.length; i++) { + const [expectedPath,inputRelativePathParam] = testMatrix[i]; + // destDirCWD = ''; + // const out = relative(inputRelativePathParam,viteConfigFile); + /** Resolve docs/.vitepress with relative path to destination dir */ + const out = resolve(viteConfigFile,inputRelativePathParam);//reverse + // const out = resolve('./',viteConfigFile); + // console.log(import.meta.url); + // const tmp = fileURLToPath(new URL('../..', import.meta.url)) + assert.strictEqual(out,expectedPath); + } + + + //assert.strictEqual(1,1);//require assert + }); + it('reverse use case', function(){ + for (let i = 0; i < testMatrix.length; i++) { + const [inputDestinationPath,expectedRelativePath] = testMatrix[i]; //reversed + const out = relative(viteConfigFile, inputDestinationPath);// some of the relative paths may not work + assert.strictEqual(out,expectedRelativePath); + } + + + //assert.strictEqual(1,1);//require assert + }); + +}); diff --git a/docs/src/readme_vite_press.md b/docs/src/readme_vite_press.md index c80a414..af98a84 100644 --- a/docs/src/readme_vite_press.md +++ b/docs/src/readme_vite_press.md @@ -2,6 +2,41 @@ 1. Need to use vscode / webstorm to inject the header or w/e * i think there is also called a snippet / wrapper / file template + +## Install +```bash +pnpm add -D vitepress +pnpm dlx vitepress init +``` +```gitignore +/docs/.vitepress/cache/ +/docs/.vitepress/dist/ +``` +```js +import { defineConfig } from 'vitepress' +//append to generated: +export default defineConfig({ + base:"/material-design-3-import-export-ext/", + srcDir: './src',//relative to the package.json vitepress dev + lang: 'en-ca', + //section inside + themeConfig: { + //add better search. https://vitepress.dev/reference/default-theme-search#local-search + search: { + provider: 'local' + }, + socialLinks: [ + { icon: 'github', link: 'https://github.com/codeforwings/material-design-3-import-export-ext' } + ], + markdown:{ + //https://vitepress.dev/reference/site-config#markdown + lineNumbers: true, + space_size: 2,//not sure if this works + } + } +}) + +``` # Concepts * "On this page" * Edit on GitHub @@ -29,11 +64,7 @@ Notes --- -## Git Ignore -```gitignore -/docs/.vitepress/cache/ -/docs/.vitepress/dist/ -``` + # Syntax Highlighting [https://github.com/shikijs/shiki/blob/main/docs/languages.md](https://github.com/shikijs/shiki/blob/main/docs/languages.md) ```js diff --git a/docs/src/vitepress/readme_vite_press_paths.md b/docs/src/vitepress/readme_vite_press_paths.md index 129aaa2..306f3f3 100644 --- a/docs/src/vitepress/readme_vite_press_paths.md +++ b/docs/src/vitepress/readme_vite_press_paths.md @@ -85,4 +85,10 @@ docs/src/public/ <<< @/public/some_folder/some_file.json -``` \ No newline at end of file +``` + +## Interesting +```ps1 +# mixing paths actually works in powershell and pwd... with no spaces at least and explorer... crazy +cd C:\Users\Public/bins +``` diff --git a/lib/bin_build/bin-vite.config.mjs b/lib/bin_build/bin-vite.config.mjs new file mode 100644 index 0000000..c98114d --- /dev/null +++ b/lib/bin_build/bin-vite.config.mjs @@ -0,0 +1,36 @@ +/** + * Bin builder for vite config + */ +// Utilities +import { defineConfig } from 'vite' +import { fileURLToPath, URL } from 'node:url' +import {rootFileUrl, viteBaseConfig} from "##/vite.config.mjs"; +// import { resolve } from 'node:path' +// https://vitejs.dev/config/ +export default defineConfig({ + ...viteBaseConfig, + build: { + //https://vitejs.dev/guide/build.html#library-mode + outDir: fileURLToPath(new URL('./lib/bin_build/.output/', rootFileUrl)), + lib: { + name: 'nuxt3-win32-posix-path', + formats: ['es', 'cjs'],//('es' | 'cjs' | 'umd' | 'iife') + entry: [ //"entry" can be a dictionary or array of multiple entry points + fileURLToPath(new URL('./lib/bin_build/index.mjs', rootFileUrl)), + ] + }, + rollupOptions: { + // make sure to externalize deps that shouldn't be bundled + // into your library + // external: ['puppeteer',"puppeteer-core","node:path/posix","node*"], + output: { + // Provide global variables to use in the UMD build + // for externalized deps + // globals: { + // vue: 'Vue',//not sure what this means + // }, + }, + }, + }, + +}) diff --git a/lib/bin_build/index.mjs b/lib/bin_build/index.mjs new file mode 100644 index 0000000..2188fd3 --- /dev/null +++ b/lib/bin_build/index.mjs @@ -0,0 +1,22 @@ +/** + * Bin Build Index Packager + * maybe add cli builder / handler here + lib/bin_build/dist/index-win.exe c:/Users/Public/Documents + + node lib/bin_build/index.mjs c:/Users/Public/Documents + node lib/bin_build/index.mjs "c:/Users/Public/Docum ents" + node lib/bin_build/index.mjs c:/Users/Public/Docum ents + * + * todo think about how to test this properly performance wise + * it's surprisingly not bad. powershell was much slower... + */ +// import process from "node:process"; +import {win32ToWin32WSL2} from "##/src/win32ToWin32WSL/win32ToWin32WSL2.mjs"; +// export {win32ToWin32WSL2} + +/* take arguments from command line */ +const args = process.argv.slice(2); +// console.log(args); +// console.log(win32ToWin32WSL2(args[0])) +console.log(win32ToWin32WSL2(args.join(' '))) +process.exit(0) diff --git a/lib/test-utils/mocha-cli-exec.mjs b/lib/test-utils/mocha-cli-exec.mjs new file mode 100644 index 0000000..e888e02 --- /dev/null +++ b/lib/test-utils/mocha-cli-exec.mjs @@ -0,0 +1,37 @@ +import {spawnSync} from "node:child_process"; +import assert from "node:assert"; + +/** + * factory function + */ +/** + * + * @param pathToExe + * @param args + * @param options + * @return {(function(*, *): void)|*} + * @example + * const assertW2Wb = createMochaCliExe(W2WB);1 + */ +export function createMochaCliExe(pathToExe,args=[],options={}){ + //todo add better defaulting etc... didnt think i neeeded to modify the input + const actualOptions = {shell:true,...options} + + return function(expectedValue,...inputToTest){ + const output = spawnSync( + // `"${W2WB}" [C:\\` //cmd needs to double quote + // `"${pathToExe}"`,[WSLPassTests[0][0]],actualOptions + `"${pathToExe}"`,[...inputToTest],actualOptions + ); + if(output.status !== 0){ + console.error(output); + console.log(output.stdout.toString()) + console.error(output.stderr.toString()) + throw new Error("status not 0"); + } + const actual = output.stdout.toString().trim(); + console.assert(actual === expectedValue,`inputToTest: ${inputToTest}`) + assert.strictEqual(actual,expectedValue); + } + +} \ No newline at end of file diff --git a/lib/test-utils/pwsh-test-mocha.ps1 b/lib/test-utils/pwsh-test-mocha.ps1 new file mode 100644 index 0000000..7350ff4 --- /dev/null +++ b/lib/test-utils/pwsh-test-mocha.ps1 @@ -0,0 +1,16 @@ +#!pwsh.exe +<# +todo move this somewhere +Test powershell inside of mocha... source and import? +cd C:\Users\Jason\WebstormProjects\nuxt3-win32-posix-path +pwsh.exe -File pwsh-test-mocha.ps1 c:\hi +pwsh.exe -File lib/test-utils/pwsh-test-mocha.ps1 c:\hi s +#> + +# import-module -Name ./src/win32ToWin32WSL/win32ToWin32WSL2.ps1 -Function pathWin32ToPosix +# source +# powershell -Command ./src/win32ToWin32WSL/win32ToWin32WSL2.ps1; pathWin32ToPosix 'c:\hi' +#write-host -NoNewline $args[0] +. ./src/win32ToWin32WSL/win32ToWin32WSL2.ps1; +$tmp=pathWin32ToPosix $args[0] +write-host -NoNewline $tmp diff --git a/package.json b/package.json index 96d6488..eccb171 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,11 @@ "docs:dev": "vitepress dev docs", "docs:build": "vitepress build docs", "docs:preview": "vitepress preview docs", - "build-rollup:vite": "vite build --mode production" + "build-rollup:vite": "vite build --mode production", + "build-rollup:pkg": "pkg lib/dist/index.js --out-path lib/dist/bins", + "build-bins" : "pnpm run /build-bins:.*/", + "build-bins:rollup": "vite build --mode production --config lib/bin_build/bin-vite.config.mjs", + "build-bins:pkg": "pkg lib/bin_build/.output/index.js --out-path lib/bin_build/dist/" }, "devDependencies": { "gh-pages": "^4.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 404a25b..fc6fbdf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3865,8 +3865,8 @@ packages: - utf-8-validate dev: true - /node-abi@3.43.0: - resolution: {integrity: sha512-QB0MMv+tn9Ur2DtJrc8y09n0n6sw88CyDniWSX2cHW10goQXYPK9ZpFJOktDS4ron501edPX6h9i7Pg+RnH5nQ==} + /node-abi@3.45.0: + resolution: {integrity: sha512-iwXuFrMAcFVi/ZoZiqq8BzAdsLw9kxDfTC0HMyjXfSL/6CSDAGD5UmR7azrAgWV1zKYq7dUUMj4owusBWKLsiQ==} engines: {node: '>=10'} dependencies: semver: 7.3.8 @@ -4662,7 +4662,7 @@ packages: minimist: 1.2.8 mkdirp-classic: 0.5.3 napi-build-utils: 1.0.2 - node-abi: 3.43.0 + node-abi: 3.45.0 pump: 3.0.0 rc: 1.2.8 simple-get: 4.0.1 diff --git a/src/cli/cli-index.test.mjs b/src/cli/cli-index.test.mjs new file mode 100644 index 0000000..d394b6d --- /dev/null +++ b/src/cli/cli-index.test.mjs @@ -0,0 +1,245 @@ +/** + yarn add mocha -D + + package.json + "imports": { + "##/*": { + "default": "./*" + }, + }, + "type": "module", + + jsconfig.json + { + "compilerOptions": { + "baseUrl": ".", + "paths": { + "##/*": ["./*"] + } + }, + "exclude": ["node_modules", ".nuxt", "dist"] + } + + + + */ +// import { createRequire } from 'module'; +// const require = createRequire(import.meta.url); +// const assert = require('assert'); +// const {describe,it} = require('mocha'); +import assert from 'node:assert'; +import {describe, it} from 'mocha'; +/* +1. +yarn add mocha @babel/polyfill @babel/register @babel/preset-env babel-plugin-module-resolver --dev +yarn add @babel/core --dev +2. +-r @babel/register -r babel-plugin-module-resolver + +3. +.babelrc +{ + + "presets": ["@babel/preset-env"], + "plugins": [ + ["module-resolver", { + "root": ["./src"], + "alias": { + "test": "./test", + "underscore": "lodash", + + "~": "./" + } + }] + ] + +} +test specific timeout +this.timeout(500);//500ms +*/ +/** + * Should put this somewhere safe + * todo filepath needs to be initialized as well... + * @param fileName .json + * @param data will automatically be changed + */ +import fs from 'node:fs'; +import {spawnSync} from "node:child_process"; +import os from "node:os"; +import {win32ToWin32WSL2} from "##/src/index.mjs"; + +function writeToFile(fileName,data,space=2){ + const sFileName = /\./.test(fileName) ? fileName : fileName + '.json'; + const filePath = `dev/pbs/test/${sFileName}` + fs.writeFileSync(filePath, + typeof data === 'string' ? data :JSON.stringify(data,null,+space) + ); +} + +/** + * interesting... didnt work for linux and mac... but for wsl? is it the shell? + * + lib/dist/bins/index-win.exe C:\\Users\\Public\\bins #pwsh + "lib/dist/bins/index-win.exe" "C:\\Users\\Public\\bins" #cmd + "./lib/dist/bins/index-win.exe" "C:\\Users\\Public\\bins" #cmd + ./"lib/dist/bins/index-win.exe" "C:\\Users\\Public\\bins" #wsl fish ? + lib/dist/bins/index-linux "'C:\\Users\\Public\\bins'" # wsl / linux + lib/dist/bins/index-macos "'C:\\Users\\Public\\bins'" # macos + * + */ +describe('cli-index.test.mjs all',function(){ + it('raw js cmd',function(){ + const sInput = "C:\\Users\\Public\\bins" + const expected = "/mnt/c/Users/Public/bins" + const out = win32ToWin32WSL2(sInput); + assert.strictEqual(out,expected) + }) +}); +describe('cli-index.test.mjs', function(){ + it('verify command line mjs', function(){ + //assert.strictEqual(1,1);//require assert + const expected = "/mnt/c/Users/Public/bins\n" + const out = spawnSync('node src/cli/index.mjs',["C:\\Users\\Public\\bins"],{shell:true}) + const stdout = out.stdout.toString(); + if(out.status !==0){ + console.error(out.stderr.toString()) + throw out.error; + } + assert.strictEqual(stdout,expected) + + }); + it('verify command line mjs error', function(){ + //assert.strictEqual(1,1);//require assert + const out = spawnSync('node src/cli/index.mjs',[],{shell:true}) + // console.log(out.stdout.toString()); + if(out.status !==0){ + assert.ok(true) + // console.error(out.stderr.toString()) + // throw out.error; + } + }); + +}); +/** + * Didn't even need the index-linux... because it worked with the exe + */ +describe('cli-index.test.mjs binary win', function(){ + // const binaryPath= "lib/dist/bins" + const binaryCommand = '"lib/dist/bins/index-win.exe"' + const binaryCommandWSL = './lib/dist/bins/index-linux' + // const binaryCommandWSL = './lib/dist/bins/index-win.exe';//also worked... + if(os.platform() !== 'win32'){ + return true; + } + + it('verify command line mjs', function(){ + //assert.strictEqual(1,1);//require assert + const expected = "/mnt/c/Users/Public/bins\n" + const out = spawnSync(binaryCommand,["C:\\Users\\Public\\bins"],{shell:true}) + const stdout = out.stdout.toString(); + if(out.status !==0){ + console.error(out.stderr.toString()) + throw out.error; + } + assert.strictEqual(stdout,expected) + + }); + it('verify command line wsl', function(){ + //assert.strictEqual(1,1);//require assert + const expected = "/mnt/c/Users/Public/bins\n" + // const out = spawnSync('wsl -e',[binaryCommandWSL,"C:\\Users\\Public\\bins"],{shell:true}) + const out = spawnSync('wsl -e',[binaryCommandWSL,"'C:\\Users\\Public\\bins'"],{shell:true}) + const stdout = out.stdout.toString(); + if(out.status !==0){ + console.error(out.stderr.toString()) + throw out.error; + } + assert.strictEqual(stdout,expected) + + }); + it('verify command line mjs error', function(){ + //assert.strictEqual(1,1);//require assert + const out = spawnSync(binaryCommand,[],{shell:true}) + // console.log(out.stdout.toString()); + if(out.status !==0){ + assert.ok(true) + // console.error(out.stderr.toString()) + // throw out.error; + } + }); + +}); + + +/** + * Don't need this binary. just testing it + * interestingly... it failed + */ +describe('cli-index.test.mjs binary mac', function(){ + // const binaryPath= "lib/dist/bins" + let binaryCommand = './lib/dist/bins/index-macos';// + // binaryCommand = 'lib/dist/bins/index-macos' //this worked too + binaryCommand = './lib/dist/bins/index-macos' //interesting + if(os.platform() !== 'darwin'){ + return true; + } + + it('verify command line mjs', function(){ + //assert.strictEqual(1,1);//require assert + const expected = "/mnt/c/Users/Public/bins\n" + const out = spawnSync(binaryCommand,["'C:\\Users\\Public\\bins'"],{shell:true}) + const stdout = out.stdout.toString(); + if(out.status !==0){ + console.error(out.stderr.toString()) + throw out.error; + } + assert.strictEqual(stdout,expected) + + }); + it('verify command line mjs error', function(){ + //assert.strictEqual(1,1);//require assert + const out = spawnSync(binaryCommand,[],{shell:true}) + // console.log(out.stdout.toString()); + if(out.status !==0){ + assert.ok(true) + // console.error(out.stderr.toString()) + // throw out.error; + } + }); + +}); +/** + * Don't need this binary. just testing it + * interestingly... it failed + */ +describe('cli-index.test.mjs binary deb', function(){ + // const binaryPath= "lib/dist/bins" + let binaryCommand = './lib/dist/bins/index-linux';// + if(os.platform() === 'linux'){ + return true; + } + + it('verify command line mjs', function(){ + //assert.strictEqual(1,1);//require assert + const expected = "/mnt/c/Users/Public/bins\n" + const out = spawnSync(binaryCommand,["'C:\\Users\\Public\\bins'"],{shell:true}) + const stdout = out.stdout.toString(); + if(out.status !==0){ + console.error(out.stderr.toString()) + throw out.error; + } + assert.strictEqual(stdout,expected) + + }); + it('verify command line mjs error', function(){ + //assert.strictEqual(1,1);//require assert + const out = spawnSync(binaryCommand,[],{shell:true}) + // console.log(out.stdout.toString()); + if(out.status !==0){ + assert.ok(true) + // console.error(out.stderr.toString()) + // throw out.error; + } + }); + +}); \ No newline at end of file diff --git a/src/cli/index.mjs b/src/cli/index.mjs new file mode 100644 index 0000000..ff6d172 --- /dev/null +++ b/src/cli/index.mjs @@ -0,0 +1,17 @@ +import {win32ToWin32WSL2} from "##/src/index.mjs"; + +/** + * pnpm install has-flag -D + * https://github.com/sindresorhus/has-flag + * if flags are needed + */ +const args = process.argv; +const [nodePath,scriptPath,...scriptArgs] = args; +// console.log(scriptArgs); +/* lazy validation*/ +if(scriptArgs.length !== 1){ + console.error('error: invalid args'); + process.exit(1); +} +const wslPath = win32ToWin32WSL2(scriptArgs[0]) +console.log(wslPath);//returns \n, might not want that diff --git a/src/index.mjs b/src/index.mjs index 912bbc4..8aeb80c 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -6,7 +6,7 @@ import {pathWin32ToPosix,pathPosixToWin32} from "#src/pathReplacement.mjs"; export {pathWin32ToPosix,pathPosixToWin32} -import {win32ToWin32WSL2,win32ToWin32Slash} from "#src/win32ToWin32WSL2.mjs"; +import {win32ToWin32WSL2,win32ToWin32Slash} from "#src/win32ToWin32WSL/win32ToWin32WSL2.mjs"; export {win32ToWin32WSL2,win32ToWin32Slash} import {win32ToWin32JS} from "#src/win32ToWin32JS.mjs"; diff --git a/src/win32ToWin32WSL2.mjs b/src/win32ToWin32WSL/win32ToWin32WSL2.mjs similarity index 91% rename from src/win32ToWin32WSL2.mjs rename to src/win32ToWin32WSL/win32ToWin32WSL2.mjs index 356fda5..f6922a3 100644 --- a/src/win32ToWin32WSL2.mjs +++ b/src/win32ToWin32WSL/win32ToWin32WSL2.mjs @@ -4,6 +4,9 @@ * * ths might work? * pathWin32ToPosix + * + * doesnt make much sense though... should just convert to ps1 / bash and run test on it. + * exe is too big, also has to go to stdout... */ // import {pathWin32ToPosix} from "./pathReplacement.mjs"; import {pathWin32ToPosix} from "#src/pathReplacement.mjs"; diff --git a/src/win32ToWin32WSL/win32ToWin32WSL2.ps1 b/src/win32ToWin32WSL/win32ToWin32WSL2.ps1 new file mode 100644 index 0000000..2443f7e --- /dev/null +++ b/src/win32ToWin32WSL/win32ToWin32WSL2.ps1 @@ -0,0 +1,18 @@ +<# + pwsh.exe -File src\win32ToWin32WSL\win32ToWin32WSL2.ps1 'C:\Users\username\Downloads\' +#> +function pathWin32ToPosix { # + param($inPathWin32,$spaceEscape = "\ ") # + $inPathWin32 = $inPathWin32.trim() # + $inPathWin32 = $inPathWin32.Replace('\\','\') + $inPathWin32 = $inPathWin32.Replace('\','/') + $inPathWin32 = $inPathWin32.Replace(' ',$spaceEscape) # space escape + $inPathWin32 = $inPathWin32 -replace "^[`"`']", '' # remove leading quotes + $inPathWin32 = $inPathWin32 -replace "[`"`']$", '' # remove ending quotes + + # .replace(/^["']/,'') # + # .replace(/["']$/,'') # + return $inPathWin32 # +} +#clear-host +#pathWin32ToPosix 'C:/Users/username/Downloads/' # "C:/Users/username/Downloads/" \ No newline at end of file diff --git a/tests/concepts/nodePath.test.mjs b/tests/concepts/nodePath.test.mjs index 841f6a6..f213b83 100644 --- a/tests/concepts/nodePath.test.mjs +++ b/tests/concepts/nodePath.test.mjs @@ -78,8 +78,9 @@ function writeToFile(fileName,data,space=2){ import {posix,win32,sep} from 'node:path' -import {pathWin32ToPosix,pathPosixToWin32} from "../../src/pathReplacement.mjs" -import {posixTests, win32Tests} from "../../lib/nodePathTestExamples.mjs"; +import {pathWin32ToPosix,pathPosixToWin32} from "##/src/pathReplacement.mjs" +import {posixTests, win32Tests} from "##/lib/nodePathTestExamples.mjs"; +import {spawnSync} from "node:child_process"; /** * mobaxterm is /drives/c or cd c:/ @@ -131,3 +132,44 @@ describe('nodePath.test.mjs', function(){ assert.strictEqual(sep,"\\") }) }); + +const PathWin32ToPosixTests = [ + ["C:\\Users\\Public\\Documents","C:/Users/Public/Documents"],//maybe append with quotes instead? + ["C:\\Users\\Public\\temp spaces\\a\\b c\\d","C:/Users/Public/temp\\ spaces/a/b\\ c/d"], + ["C:\\\\Users\\\\Public\\\\temp spaces\\\\a\\\\b c\\\\d","C:/Users/Public/temp\\ spaces/a/b\\ c/d"], + ["C:\\\\Users\\\\Public\\\\Documents","C:/Users/Public/Documents"] +]; +const ogLength = PathWin32ToPosixTests.length +for (let i = 0; i < ogLength; i++) { + const row = PathWin32ToPosixTests[i]; + row[2] = i; + PathWin32ToPosixTests.push([`'${row[0]}'`,row[1],`${i}1`]);//append with single quotes + PathWin32ToPosixTests.push([`"${row[0]}"`,row[1],`${i}2`]);//append with double quotes + +} +describe('PathWin32ToPosixTests', function(){ + for (let i = 0; i < PathWin32ToPosixTests.length; i++) { + // for (let i = 0; i < 1; i++) { //run only 1 + const [inputWinPath, expectedMntPath, win32ToPosixIndex] = PathWin32ToPosixTests[i]; + it(`PathWin32ToPosixTests MJS ${win32ToPosixIndex}`, function () { + // console.log(wslPassTestIndex,inputWinPath); + const actual = pathWin32ToPosix(inputWinPath); + assert.strictEqual(actual,expectedMntPath); + }); + it(`PathWin32ToPosixTests ps1 ${win32ToPosixIndex}`, function () { + // console.log(wslPassTestIndex,inputWinPath); + const output = spawnSync(`pwsh.exe -file lib/test-utils/pwsh-test-mocha.ps1`,[inputWinPath],{shell:true}); + // const output = spawnSync(`pwsh.exe -file lib/test-utils/pwsh-test-mocha.ps1`,[inputWinPath],{shell:false}); + // const output = spawnSync(`pwsh.exe -Command write-host ${inputWinPath}`,[],{shell:true}); + if(output.status !== 0){ + console.log(output.stdout.toString()) + console.error(output.stderr.toString()) + console.error(output.error,output.status) + throw output.error + } + const actual = output.stdout.toString() + + assert.strictEqual(actual,expectedMntPath); + }); + } +}); \ No newline at end of file diff --git a/tests/unit/mocha-cli-exec.mjs b/tests/unit/mocha-cli-exec.mjs new file mode 100644 index 0000000..aea332c --- /dev/null +++ b/tests/unit/mocha-cli-exec.mjs @@ -0,0 +1,89 @@ +/** +yarn add mocha -D + +package.json + "imports": { + "##/*": { + "default": "./*" + }, + }, + "type": "module", + + jsconfig.json + { + "compilerOptions": { + "baseUrl": ".", + "paths": { + "##/*": ["./*"] + } + }, + "exclude": ["node_modules", ".nuxt", "dist"] +} + + + +*/ +// import { createRequire } from 'module'; +// const require = createRequire(import.meta.url); +// const assert = require('assert'); +// const {describe,it} = require('mocha'); +import assert from 'node:assert'; +import { describe, it} from 'mocha'; +/* +1. +yarn add mocha @babel/polyfill @babel/register @babel/preset-env babel-plugin-module-resolver --dev +yarn add @babel/core --dev +2. +-r @babel/register -r babel-plugin-module-resolver + +3. +.babelrc +{ + + "presets": ["@babel/preset-env"], + "plugins": [ + ["module-resolver", { + "root": ["./src"], + "alias": { + "test": "./test", + "underscore": "lodash", + + "~": "./" + } + }] + ] + +} +test specific timeout +this.timeout(500);//500ms +*/ +/** + * Should put this somewhere safe + * todo filepath needs to be initialized as well... + * @param fileName .json + * @param data will automatically be changed + */ +import fs from 'node:fs'; +import {createMochaCliExe} from "##/lib/test-utils/mocha-cli-exec.mjs"; +function writeToFile(fileName,data,space=2){ + const sFileName = /\./.test(fileName) ? fileName : fileName + '.json'; + const filePath = `dev/pbs/test/${sFileName}` + fs.writeFileSync(filePath, + typeof data === 'string' ? data :JSON.stringify(data,null,+space) + ); +} +describe('tests/unit/mocha-cli-exec.mjs', function(){ + const assertW2bMJS = createMochaCliExe("node.exe") + const pathToJS = "lib/bin_build/index.mjs" + it('Check factory function - createMochaCliExe', function(){ + //assert.strictEqual(1,1);//require assert + const fn = createMochaCliExe() + console.log(fn.toString()); + }); + it('check index.mjs without spaces', function(){ + assertW2bMJS("/mnt/c/Users/Public/Documents",pathToJS,"c:/Users/Public/Documents"); + }); + it('check index.mjs with spaces', function(){ + assertW2bMJS("/mnt/c/Users/Public/Docu\\ ments",pathToJS,"c:/Users/Public/Docu ments"); + }); +}); diff --git a/tests/unit/win32ToWin32WSL2.test.mjs b/tests/unit/win32ToWin32WSL2.test.mjs index 0cb791c..b3ecf2e 100644 --- a/tests/unit/win32ToWin32WSL2.test.mjs +++ b/tests/unit/win32ToWin32WSL2.test.mjs @@ -65,10 +65,10 @@ this.timeout(500);//500ms */ import fs from 'node:fs'; // import {win32ToWin32WSL2} from "##/dev/win-to-wsl/win32ToWin32WSL2.mjs"; -import {win32ToWin32Slash, win32ToWin32WSL2} from "#src/win32ToWin32WSL2.mjs";//fixme check the import subpath in package.json in other branch +import {win32ToWin32Slash, win32ToWin32WSL2} from "##/src/win32ToWin32WSL/win32ToWin32WSL2.mjs";//fixme check the import subpath in package.json in other branch function writeToFile(fileName,data,space=2){ const sFileName = /\./.test(fileName) ? fileName : fileName + '.json'; - const filePath = `dev/win-to-wsl/${sFileName}` + const filePath = `temp/${sFileName}` fs.writeFileSync(filePath, typeof data === 'string' ? data :JSON.stringify(data,null,+space) ); @@ -77,6 +77,7 @@ function writeToFile(fileName,data,space=2){ /** * To integrate and move with other tests */ + describe('win32ToWin32WSL2.test.mjs', function(){ it('wsl', function(){ //assert.strictEqual(1,1);//require assert @@ -136,3 +137,80 @@ describe('win32ToWin32WSL2.test.mjs', function(){ // because im reallocating and renaming / files and folders with spaces }); + + +/** + * wsl param tests + * + */ +export const WSLPassTests = [ + ["C:\\Users\\Public\\Documents","/mnt/c/Users/Public/Documents"],//maybe append with quotes instead? + ["C:\\Users\\Public\\temp spaces\\a\\b c\\d","/mnt/c/Users/Public/temp\\ spaces/a/b\\ c/d"], + ["C:\\Users\\Public\\temp spaces\\a\\b c\\d","/mnt/c/Users/Public/temp\\ spaces/a/b\\ c/d"], + ["C:\\\\Users\\\\Public\\\\Documents","/mnt/c/Users/Public/Documents"], + //todo look into thje backtick + // ["C:\\Users\\Public\\temp` spaces\\a\\b` c\\d","/mnt/c/Users/Public/temp\\ spaces/a/b\\ c/d"], +] +//should work with changing the length of the array +const ogLength = WSLPassTests.length +for (let i = 0; i < ogLength; i++) { + const row = WSLPassTests[i]; + row[2] = i; + WSLPassTests.push([`'${row[0]}'`,row[1],`${i}1`]);//append with single quotes + WSLPassTests.push([`"${row[0]}"`,row[1],`${i}2`]);//append with double quotes + +} +// writeToFile('WSLPassTests.jsonc',WSLPassTests); +/** + * parameterized tests + */ +import {spawnSync} from "node:child_process"; +import {createMochaCliExe} from "##/lib/test-utils/mocha-cli-exec.mjs"; + +describe('WSLPassTests', function(){ + /** @type {string|'Win32ToWin32WSL2BinaryPath'} */ + const W2WB = "lib/bin_build/dist/index-win.exe"; + const assertW2Wb = createMochaCliExe(W2WB); + /* raw */ + it('WSLPassTests mocha exe', function(){ + const output = spawnSync( + // `"${W2WB}" [C:\\` //cmd needs to double quote + `"${W2WB}"`,[WSLPassTests[0][0]],{shell:true} + ); + if(output.status !== 0){ + console.error(output); + console.log(output.stdout.toString()) + console.error(output.stderr.toString()) + throw new Error("status not 0"); + } + const actual = output.stdout.toString().trim(); + assert.strictEqual(actual,WSLPassTests[0][1]); + }); + /* wrapper note reversed */ + it('WSLPassTests assertW2Wb', function(){ + assertW2Wb(WSLPassTests[0][1],WSLPassTests[0][0]); + }); + /* wrapper note reversed */ + it('WSLPassTests assertW2Wb - spaces', function(){ + assertW2Wb(WSLPassTests[1][1],WSLPassTests[1][0]); + }); + /**/ + for (let i = 0; i < WSLPassTests.length; i++) { + const [inputWinPath, expectedMntPath, wslPassTestIndex] = WSLPassTests[i]; + it(`WSLPassTests MJS ${wslPassTestIndex}`, function () { + // console.log(wslPassTestIndex,inputWinPath); + const actual = win32ToWin32WSL2(inputWinPath); + assert.strictEqual(actual,expectedMntPath); + }); + /* scrapping ps1 / sh using exe for now */ + // it(`WSLPassTests ps1 ${wslPassTestIndex}`, function () { + // // console.log(wslPassTestIndex,inputWinPath); + // const output = spawnSync(inputWinPath); + // assert.strictEqual(actual,expectedMntPath); + // }); + it(`WSLPassTests exe ${wslPassTestIndex}`, function(){ + // // console.log(wslPassTestIndex,inputWinPath); + assertW2Wb(expectedMntPath,[inputWinPath]); + }); + } +}); \ No newline at end of file diff --git a/vite.config.mjs b/vite.config.mjs index 2c1888c..cd04089 100644 --- a/vite.config.mjs +++ b/vite.config.mjs @@ -3,10 +3,18 @@ */ // Utilities import { defineConfig } from 'vite' + import { fileURLToPath, URL } from 'node:url' // import { resolve } from 'node:path' // https://vitejs.dev/config/ -export default defineConfig({ +/** + * @typedef {import('vite').UserConfigExport} UserConfigExport + */ +/** + * @type {UserConfigExport} + */ +export const viteBaseConfig = (()=>{ + return { define: { 'process.env': {} }, resolve: { alias: { @@ -31,11 +39,16 @@ export default defineConfig({ outDir: fileURLToPath(new URL('./lib/dist/', import.meta.url)), lib: { name: 'nuxt3-win32-posix-path', - formats: ['es', 'cjs','umd'],//('es' | 'cjs' | 'umd' | 'iife') + // formats: ['es', 'cjs','umd'],//('es' | 'cjs' | 'umd' | 'iife') + formats: ['es', 'cjs'],//('es' | 'cjs' | 'umd' | 'iife') //i lost my jsdocs though... weird entry: [ //"entry" can be a dictionary or array of multiple entry points fileURLToPath(new URL('./src/index.mjs', import.meta.url)), - // fileURLToPath(new URL('./dev/node-fs-utils-dev/create-dummy-files.mjs', import.meta.url)), + // fileURLToPath(new URL('./src/win32ToWin32WSL/win32ToWin32WSL2.mjs', import.meta.url)), + // fileURLToPath(new URL('./temp/toWsl.mjs', import.meta.url)), + // fileURLToPath(new URL('./src/cli/index.mjs', import.meta.url)), + // fileURLToPath(new URL('./dev/node-fs-utils-dev/create-dummy-files.mjs + // ', import.meta.url)), // fileURLToPath(new URL('./src/win32ToWin32WSL2.mjs', import.meta.url)), // fileURLToPath(new URL('./src/import-material-theme-pup.mjs', import.meta.url)), // resolve('src/import-theme-chrome-pup.mjs'), @@ -59,4 +72,8 @@ export default defineConfig({ }, }, -}) +}})(); +export const rootFilePath = fileURLToPath(new URL('./', import.meta.url)); +/** @type {string|URL} */ +export const rootFileUrl = import.meta.url; +export default defineConfig(viteBaseConfig)