Skip to content

Commit d96cf7a

Browse files
authored
Merge pull request #38 from cube-root/tailwind-local-fix
tailwind loader fix
2 parents 6a63daa + c3e04c8 commit d96cf7a

File tree

6 files changed

+90
-32
lines changed

6 files changed

+90
-32
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ npx directory-serve /path-to-file
5252
| username | undefined | Client auth username | `npx directory-serve /path-of-directory --username=my_username ` |
5353
| password | undefined | Client auth password (optional) | `npx directory-serve /path-of-directory --username=my_username --password=my_password ` |
5454
| delete | false | To delete file/folder | `npx directory-serve /path-of-directory --delete=true` |
55+
| debug | false | Debug mode | `npx directory-serve /path-of-directory --delete=true --debug=true` |
5556

5657
## Examples
5758

@@ -91,11 +92,11 @@ npm i
9192
```bash
9293
npm run dev /path-of-directory
9394
```
95+
9496
## For Contributing
9597

9698
[Contribution Guide](/docs/CONTRIBUTING.MD)
9799

98-
99100
## Screenshot
100101

101102
### CLI

bin/index.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Options
2323
--password ..... Client auth password
2424
2525
--delete ..... Delete file/folder
26+
27+
--debug ..... Debug mode
2628
* To serve a directory
2729
directory-serve /path-of-directory
2830
@@ -35,25 +37,47 @@ directory-serve /path-of-file
3537
const options = yargs
3638
.usage(yargsMessage)
3739
.option('p', {
38-
default: 8989, alias: 'port', describe: 'Change default port', type: 'integer', demandOption: false,
40+
default: 8989,
41+
alias: 'port',
42+
describe: 'Change default port',
43+
type: 'integer',
44+
demandOption: false,
3945
})
4046
.option('u', {
41-
default: true, alias: 'uploadFile', describe: 'File upload mode', type: 'boolean',
47+
default: true,
48+
alias: 'uploadFile',
49+
describe: 'File upload mode',
50+
type: 'boolean',
4251
})
4352
.options('username', {
44-
default: undefined, describe: 'Client auth username', type: 'string', demandOption: false,
53+
default: undefined,
54+
describe: 'Client auth username',
55+
type: 'string',
56+
demandOption: false,
4557
})
4658
.options('password', {
47-
default: undefined, describe: 'Client auth password', type: 'string', demandOption: false,
59+
default: undefined,
60+
describe: 'Client auth password',
61+
type: 'string',
62+
demandOption: false,
4863
})
4964
.options('delete', {
50-
default: false, alias: 'deleteFile', describe: 'Delete file/folder', type: 'boolean', demandOption: false,
65+
default: false,
66+
alias: 'deleteFile',
67+
describe: 'Delete file/folder',
68+
type: 'boolean',
69+
demandOption: false,
70+
})
71+
.options('debug', {
72+
default: false,
73+
describe: 'Debug mode',
74+
type: 'boolean',
75+
demandOption: false,
5176
})
52-
.help(true)
53-
.argv;
77+
.help(true).argv;
5478

5579
const {
56-
uploadFile, username, password, deleteFile,
80+
uploadFile, username, password, deleteFile, debug,
5781
} = options;
5882
let path = options._[0];
5983
if (!path) {
@@ -85,7 +109,8 @@ if (isFile) {
85109
* Auth
86110
*/
87111
app.use((req, res, next) => authMiddleware(req, res, next, {
88-
username, password,
112+
username,
113+
password,
89114
}));
90115
/**
91116
* SERVER
@@ -94,6 +119,7 @@ app.use((req, res) => handler(req, res, {
94119
path,
95120
uploadFile,
96121
deleteFile,
122+
debug,
97123
}));
98124

99125
app.listen(options.port, () => {

lib/helper/html.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
// const fs = require('fs-extra');
1+
const fs = require('fs-extra');
22

3-
// const cssLoader = () => fs.readFileSync('lib/helper/style.css', 'utf8');
4-
const createHtmlResponse = (body) => '<!DOCTYPE html>\n'
3+
const cssLoader = (debug) => {
4+
try {
5+
return `<style>${fs.readFileSync(`${__dirname}/style.css`, 'utf8')}</style>`;
6+
} catch (error) {
7+
if (debug) {
8+
console.log('Failed to load stylesheet');
9+
console.log(error);
10+
}
11+
}
12+
return '<script src="https://cdn.tailwindcss.com"></script>';
13+
};
14+
const createHtmlResponse = (body, { debug = false } = {}) => '<!DOCTYPE html>\n'
515
+ '<html lang="en">\n'
6-
+ '<head>\n'
7-
+ '<script src="https://cdn.tailwindcss.com"></script>'
16+
+ `<head>\n${
17+
// + '<script src="https://cdn.tailwindcss.com"></script>'
818
// + '<link rel="stylesheet" type="text/css" href="lib/helper/style.css" />'
9-
// + `<style>${cssLoader()}</style>`
10-
+ `<script type="text/javascript">
19+
cssLoader(debug)
20+
}<script type="text/javascript">
1121
function beforeFileUpload(evnt){
1222
var files = document.getElementsByName('filetoupload')[0].files;
1323
if(!(files && files.length > 0 && files[0])){

lib/middleware/directory.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ const {
88
generateDeleteButton,
99
} = require('../helper');
1010

11-
const directory = (req, res, { path, uploadFile = true, deleteFile = false } = {}) => {
12-
const stream = send(req, parseUrl(req).pathname, { index: false, root: path, dotfiles: 'allow' });
11+
const directory = (
12+
req,
13+
res,
14+
{
15+
path, uploadFile = true, deleteFile = false, debug = false,
16+
} = {},
17+
) => {
18+
const stream = send(req, parseUrl(req).pathname, {
19+
index: false,
20+
root: path,
21+
dotfiles: 'allow',
22+
});
1323
stream.on('directory', async (resp, dirPath) => {
1424
const directoryPath = appendSlash(dirPath);
1525
let htmlResponse = '';
@@ -33,33 +43,37 @@ const directory = (req, res, { path, uploadFile = true, deleteFile = false } = {
3343
htmlResponse
3444
+= '<li class="flex items-center space-x-2">'
3545
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 text-amber-500 flex-shrink-0"><path d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0016.5 9h-1.875a1.875 1.875 0 01-1.875-1.875V5.25A3.75 3.75 0 009 1.5H5.625z" /><path d="M12.971 1.816A5.23 5.23 0 0114.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 013.434 1.279 9.768 9.768 0 00-6.963-6.963z" /></svg>'
36-
+ `<a href="${encodeURIComponent(file)
37-
}">${file
38-
}</a>`
46+
+ `<a href="${encodeURIComponent(file)}">${file}</a>`
3947
+ '<div>'
40-
+ `<a target="_blank" download="${file}" class="text-blue-600 text-sm px-3 py-1 bg-blue-100 rounded-full inline-flex space-x-1" href='${encodeURIComponent(file)}'>`
48+
+ `<a target="_blank" download="${file}" class="text-blue-600 text-sm px-3 py-1 bg-blue-100 rounded-full inline-flex space-x-1" href='${encodeURIComponent(
49+
file,
50+
)}'>`
4151
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-4 h-4"><path d="M10.75 2.75a.75.75 0 00-1.5 0v8.614L6.295 8.235a.75.75 0 10-1.09 1.03l4.25 4.5a.75.75 0 001.09 0l4.25-4.5a.75.75 0 00-1.09-1.03l-2.955 3.129V2.75z" /><path d="M3.5 12.75a.75.75 0 00-1.5 0v2.5A2.75 2.75 0 004.75 18h10.5A2.75 2.75 0 0018 15.25v-2.5a.75.75 0 00-1.5 0v2.5c0 .69-.56 1.25-1.25 1.25H4.75c-.69 0-1.25-.56-1.25-1.25v-2.5z" /></svg>'
4252
+ '<span>Download</span>'
43-
+ `</a>${generateDeleteButton(file, deleteFile, 'Remove')
44-
}</div>`
53+
+ `</a>${generateDeleteButton(file, deleteFile, 'Remove')}</div>`
4554
+ '</li>';
4655
} else {
4756
htmlResponse
4857
+= '<li class="flex items-center space-x-2">'
4958
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 text-blue-500 flex-shrink-0"><path d="M19.906 9c.382 0 .749.057 1.094.162V9a3 3 0 00-3-3h-3.879a.75.75 0 01-.53-.22L11.47 3.66A2.25 2.25 0 009.879 3H6a3 3 0 00-3 3v3.162A3.756 3.756 0 014.094 9h15.812zM4.094 10.5a2.25 2.25 0 00-2.227 2.568l.857 6A2.25 2.25 0 004.951 21H19.05a2.25 2.25 0 002.227-1.932l.857-6a2.25 2.25 0 00-2.227-2.568H4.094z"/></svg>'
5059
+ `<span>${file}</span>`
51-
+ `<a class="text-blue-800 text-sm px-2 py-1 bg-blue-200 rounded-full" href="${appendSlash(req.url)}${encodeURIComponent(file)}/">`
60+
+ `<a class="text-blue-800 text-sm px-2 py-1 bg-blue-200 rounded-full" href="${appendSlash(
61+
req.url,
62+
)}${encodeURIComponent(file)}/">`
5263
+ 'Open folder'
53-
+ `</a>${generateDeleteButton(file, deleteFile, 'Remove folder')
54-
}</li>`;
64+
+ `</a>${generateDeleteButton(
65+
file,
66+
deleteFile,
67+
'Remove folder',
68+
)}</li>`;
5569
}
5670
});
5771
htmlResponse += '</ul></section>';
5872
resolve(htmlResponse);
5973
});
6074
});
6175
resp.setHeader('Content-Type', 'text/html; charset=UTF-8');
62-
return resp.end(createHtmlResponse(htmlResponse));
76+
return resp.end(createHtmlResponse(htmlResponse, { debug }));
6377
});
6478
stream.pipe(res);
6579
};

lib/middleware/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const { authMiddleware } = require('./auth');
77
const handler = (
88
req,
99
res,
10-
{ path, uploadFile = true, deleteFile = false } = {},
10+
{
11+
path, uploadFile = true, deleteFile = false, debug = false,
12+
} = {},
1113
) => {
1214
if (
1315
req.query.file
@@ -34,7 +36,12 @@ const handler = (
3436
});
3537
}
3638

37-
return directory(req, res, { path, uploadFile, deleteFile });
39+
return directory(req, res, {
40+
path,
41+
uploadFile,
42+
deleteFile,
43+
debug,
44+
});
3845
};
3946

4047
module.exports = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "directory-serve",
3-
"version": "1.3.5",
3+
"version": "1.3.6",
44
"description": "Command line tool to share the directory",
55
"main": "./bin/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)